简体   繁体   中英

How to use for loop with series in MATLAB

I have here an equation that is in the image

方程

I'm trying to make the plot where y-axis is this equation, and x-axis is time - just a vector. All the initials I have:

%Initials
Beta=[24 123 117 262 108 45]*10^-5; %pcm
Lambda=[0.0127 0.0317 0.1160 0.3106 1.4006 3.8760]; %1/s
LAMBDA=10^-4 ;   %s
W=[ 0.376 -0.0133 -0.0426 -0.153  -0.972 -3.38 -29.5]
Rho=400*10^-5
t=linspace(1,30,7)

This is the code I'm using:

for n=1:7
    for j=1:6
    S1=Rho*sum(exp(W(n)'.*t)/(W(n)'.*(LAMBDA+(sum(Beta(j).*Lambda(j)./(W(n)+Lambda(j))^2)))))
    end
end
semilogy(t,S1,'b','linewidth',2); 

And S1 returns too much answers, and as I undestand it should give only 7... And I am new to matlab and coding in general, so if the answer is obviuos I still don't know how to make it work :D

Let's clarify a few things first.

In order to do a 2D plot (of any kind), you need two vectors in Matlab. They should be of equal length. One for all the x-coordinates. Another for all the y-coordinates.

You get the x-coordinates in t=linspace(1,30,7) . However, you do not have the corresponding y-coordinates.

In your case, it is best to phrase your formula as a function of t . And let's break down the sums for clarity. For example,

function num = oscillation_modes_of_sort(t)
    outer_sum = 0;
    for j=1:numel(W)        
        inner_sum = 0;
        for i=1:numel(Beta)
            inner_sum = inner_sum + Beta(i)*Lambda(i)/(W(j)+Lambda(i))^2;
        end
        outer_sum = out_sum + exp(W(j)*t)/(W(j)*(LAMBDA+inner_sum));
    end
    num = Rho * outer_sum;
end

Now, your y-coordinates will be oscillation_modes_of_sort(t) .

There are ways to either make the code more brief or make it more friendly if W and Beta are much much longer. But let's do those at a future time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM