[英]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.我试图绘制其中 y 轴是这个方程,x 轴是时间 - 只是一个向量的图。 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并且 S1 返回了太多的答案,我不明白它应该只给出 7 ......而且我是 matlab 和编码的新手,所以如果答案是显而易见的,我仍然不知道如何使它工作:D
Let's clarify a few things first.让我们先澄清一些事情。
In order to do a 2D plot (of any kind), you need two vectors in Matlab.为了进行二维绘图(任何类型),您需要在 Matlab 中使用两个向量。 They should be of equal length.
它们的长度应该相等。 One for all the x-coordinates.
一个用于所有 x 坐标。 Another for all the y-coordinates.
另一个用于所有 y 坐标。
You get the x-coordinates in t=linspace(1,30,7)
.您可以在
t=linspace(1,30,7)
获得 x 坐标。 However, you do not have the corresponding y-coordinates.但是,您没有相应的 y 坐标。
In your case, it is best to phrase your formula as a function of t
.在您的情况下,最好将您的公式表述为
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)
.现在,你的 y 坐标将是
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.如果
W
和Beta
更长,则有一些方法可以使代码更简洁或使其更友好。 But let's do those at a future time.但让我们在未来的时间做这些。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.