Put result in vector

I work on this code and I want to put the result (u) for each (t) into a vector.
function THA(n,E,I,m,L,P0)
vn=1:1:n;%Antal modes
w=1/(2*pi)*vn.^2*pi^2/L^2*(E*I/m)^(1/2); %Egenfrekvens
% u=C*sin(i*pi*x/L)/i^4*(1-cos(w(i)*t))*sin(i*pi*x/L);
for t=0:0.1:10;
x=L/2;
i=1:1:n;
C=2*P0*L^3/(pi^4*E*I);
vm=sin(i*pi*x/L);
u=C*sum(vm(i).*1/vn(i).^4*(1-cos(w(i)*t)).*vm);
end
end
Thanks in advance

回答(1 个)

I don't believe you need to do the above with a for loop, just create a t vector and work with arrays, for example:
t = 0:0.1:10;
vm = sin(2*pi*t);
and then you can use cumsum() in your u.
But if you want to stay with your for loop, then just fill the vector u as you progress through the loop
u(i) = C*sum(vm(i).*1/vn(i).^4*(1-cos(w(i)*t)).*vm);
Before you enter the for loop, you may want to include the statement
u = zeros(n,1);
or
u = zeros(1,n);
to preallocate the vector.

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by