Euler integration for n dimensional system

Hi everyone, I created the following Matlab Function to solve one-dimensional system:
function f2= Euler8(f,t,y,h)
r=numel(t);
d=zeros(1,r);
d(1)=y;
for j=1:(r-1)
soln=y+h*f(t(j),y);
y=soln;
d(j+1)=y;
end
f2=d;
end
and I did the following changes to make it work for n dimensional system
function f2= Euler9(f,t,y,h)
r=numel(t);
d=zeros(1,r);
fz=numel(f);
d(fz,1)=y;
for j=1:(r-1)
for z=1:fz
soln=y+h*f(t(j),y);
y=soln;
d(:,j+1)=y;
end
end
f2=d;
end
In order to test Euler9, I wrote the following script
clear;
clc;
h=0.1;
t=0:h:1;
y(1,1)=0.16;
y(2,1)=0;
k=90;
m=10;
l=1;
c=sqrt(4*m*k*l);
f={@(y)(y(i+1,1)+y(i+1,2)*h);@(y)(y(i+1,2)+(y(i+1,2)*(-c/m)+y(i+1,1)*(-k/m)))*h};
Euler9(f,t,y,h)
but I get this error message when I run the script
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in Euler9 (line 5)
d(fz,1)=y;
Error in Euler9script (line 13)
Euler9(f,t,y,h)
could anyone please tell me what's wrong and help me to modify the script
Thanks in advance
Lamees

回答(1 个)

Looks like you are assigning a vector to a scalar spot with this line:
d(fz,1)=y;
Try this instead:
d(1:fz,1)=y;
And then you probably want to change your allocation also. E.g.,
r=numel(t);
fz=numel(f);
d=zeros(fz,r);
d(1:fz,1)=y;

类别

帮助中心File Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by