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개)

Wayne King
Wayne King 2012년 3월 9일

0 개 추천

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에 대해 자세히 알아보기

태그

질문:

2012년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by