Put result in vector
Show older comments
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
Answers (1)
Wayne King
on 9 Mar 2012
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.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!