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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by