Vectors must be the same lengths
Afficher commentaires plus anciens
Hi there, I made equation with the Euler method and the Heun method. For an assignment I have to compare these two graphs. The problem is, when I take step size "dt=0.02" everything is okay.
But I also need graphs with stepsize "dt=0.2" and an error came up: "Vectors must be the same lengths"
Here is my m-file:
m=21974;
k=87896;
c=54935;
dt=0.2;
t= 0:dt:10;
lt=length(t);
y(1)=1;
z(1)=0;
% formula Euler
tic
for n=1:lt-1
y(n+1)=y(n)+(z(n)*dt)*(k/m);
z(n+1)=z(n)-(y(n)+z(n))*dt*(c/m);
end
%plot(t,y);
%plot(t,z);
toc
% formula Heun
for n=1;lt-1
y(n+1)=y(n)+(0.5*(z(n))*(k/m)+y(n+1))*dt;
z(n+1)=z(n)-(0.5*(y(n))*(c/m)+z(n+1))*dt;
end
%plot(t,y);
plot(t,z);
I hope you guys can figure it out. None of my teachers are available right now.
Réponses (2)
Mischa Kim
le 27 Mar 2014
Modifié(e) : Mischa Kim
le 27 Mar 2014
Daan, turn the script into a function and you should be good. As the first line in your code, add
function myEuler() % or some other function name
and end the function with the end statement:
end
Save the file under the same name as your function ( myEuler.m in my case).
Daan
le 27 Mar 2014
0 votes
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!