Storing column vectors over a while loop
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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))
0 commentaires
Réponses (1)
David Hill
le 8 Oct 2019
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))
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!