Vectors must be the same lengths

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.

Antworten (2)

Mischa Kim
Mischa Kim am 27 Mär. 2014
Bearbeitet: Mischa Kim am 27 Mär. 2014

0 Stimmen

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
Daan am 27 Mär. 2014

0 Stimmen

Thank you it worked!

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 27 Mär. 2014

Beantwortet:

am 27 Mär. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by