Storing column vectors over a while loop

조회 수: 10 (최근 30일)
Juan Pablo  Arango
Juan Pablo Arango 2019년 10월 8일
답변: David Hill 2019년 10월 8일
Hi I'm having problems with my code. When it goes to the second while loop it does not record the values(.i.e it stops giving me the error that array position values most be logical or positive integers). Any hints on how to solve this?
N = 100
B = [-30 -10 0 0; -10 -30 -10 0; 0 -10 -30 -10; 0 0 -10 -30];
h = 50/2N ;
n = 1;
x = zeros(4,10/h);
x(:,1) = [1;0;0;1];
euclidean_norm(n) = 0;
time(n) = 0;
time_end = 10;
while n<=3
time(n+1) = time(n)+h;
x(:,n+1) = x(:,n) + h*B*x(:,n);
euclidean_norm(n+1) = norm(x(:,n));
n=n+1;
end
while time(n)+h < time_end + eps(1)
time(n+1) = time(n)+h;
x(:,n+1) = x(:,n) + h((23/12)*B*x(:,n)-(16/12)*B*x(:,n-1)+(5/12)*B*x(:,n-2));
euclidean_norm(n+1) = euclidean_norm(x(:,n+1));
n = n + 1;
end
plot(time(1:n),euclidean_norm(1:n))

답변 (1개)

David Hill
David Hill 2019년 10월 8일
N = 100;
B = [-30 -10 0 0; -10 -30 -10 0; 0 -10 -30 -10; 0 0 -10 -30];
h = 50/(2*N);%I believe this is what you wanted.
n = 1;
x = zeros(4,10/h);
x(:,1) = [1;0;0;1];
euclidean_norm(n) = 0;
time(n) = 0;
time_end = 10;
while n<=3
time(n+1) = time(n)+h;
x(:,n+1) = x(:,n) + h*B*x(:,n);
euclidean_norm(n+1) = norm(x(:,n));
n=n+1;
end
while time(n)+h < time_end + eps(1)
time(n+1) = time(n)+h;
x(:,n+1) = x(:,n) + h*((23/12)*B*x(:,n)-(16/12)*B*x(:,n-1)+(5/12)*B*x(:,n-2));%Need h*
euclidean_norm(n+1) = norm(x(:,n+1));%I believe you want norm(x(:,n+1)) instead of euclidean_norm which causes error.
n = n + 1;
end
plot(time(1:n),euclidean_norm(1:n))

카테고리

Help CenterFile 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