Storing column vectors over a while loop
显示 更早的评论
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
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))
类别
在 帮助中心 和 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!