要在 Octave/Matlab 中运行的输入矩阵的维度
Dimensions of input matrix to function in Octave/Matlab
我正在尝试利用我自己的 Euler 函数在 Octave 中使用常规 Euler 方法求解小型 ODE 系统。我让它适用于 1x1 矩阵输入,但我也想将它用于 2x2 矩阵输入。
我的代码如下所示:
%Euler method for an ODE system of 2x2 matrix input (equation 1) and 1x1 input (equation 2)
function [t,x,y]=Euler(f1,f2,t0,tf,x0,y0,n)
h=(tf-t0)/n; %Constant step size
t=t0:h:tf;
x=[x0 x0] ; y=[y0]; %Starting values
for j=1:n
keulerf1=f1(t(j),x,y); %f1=dx/dt (from first equation) % I think the problem with dimensions might be here****
keulerf2=f2(t(j),x,y); %f2=dy/dt (from second equation)
x=x+h*keulerf1; %Euler forward method for first variable
y=y+h*keulerf2; %Euler forward method for second variable
OUT=[t(j+1) x y]
endfor
endfunction
示例:
%Initial values:
t0=0;tf=3;
x0=10; %x(t0)
y0=10; %y(t0)
n=3;
OUT_0=[t0 x0 x0 y0 y0]
f1=@(t,x,y) [2*x 0]; %Equation 1 (arbitrary example): [dx/dt]=[2x 0]
f2=@(t,x,y) [2*y]; %Equation 2 (arbitrary example): [dy/dt]=[2y]
[t,x,y]=Euler(f1,f2,t0,tf,x0,y0,n)
%It only works for f1, f2 of 1x1 size. I don't quite know why the dimensions aren't being consistent.
感谢任何反馈。祝你有个愉快的一天。
如果我理解正确,你应该为你的 f1 使用矩阵注释 A*x
这是欧拉:
function [t,x,y]=Euler(f1,f2,t0,tf,x0,y0,n)
h=(tf-t0)/n; %Constant step size
t=t0:h:tf;
x=[x0]; % also define your two starting values better outside this function
y=[y0]; %Starting values
for j=1:n
keulerf1=f1(t(j),x,y); %f1=dx/dt (from first equation)
keulerf2=f2(t(j),x,y); %f2=dy/dt (from second equation)
x=x+h*keulerf1; %Euler forward method for first variable
y=y+h*keulerf2; %Euler forward method for second variable
OUT=[t(j+1) x' y']
endfor
endfunction
这将是你的职能:
%Initial values:
t0=0;tf=3;
x0=[10 10]'; %x(t0)
y0=10; %y(t0)
n=3;
OUT_0=[t0; x0; y0; y0]
f1=@(t,x,y) [2 0; 0 0]*x; %Equation 1 (arbitrary example): [dx/dt]=A*x
f2=@(t,x,y) [2*y]; %Equation 2 (arbitrary example): [dy/dt]=[2y]
[t,x,y]=Euler(f1,f2,t0,tf,x0,y0,n)
我正在尝试利用我自己的 Euler 函数在 Octave 中使用常规 Euler 方法求解小型 ODE 系统。我让它适用于 1x1 矩阵输入,但我也想将它用于 2x2 矩阵输入。
我的代码如下所示:
%Euler method for an ODE system of 2x2 matrix input (equation 1) and 1x1 input (equation 2)
function [t,x,y]=Euler(f1,f2,t0,tf,x0,y0,n)
h=(tf-t0)/n; %Constant step size
t=t0:h:tf;
x=[x0 x0] ; y=[y0]; %Starting values
for j=1:n
keulerf1=f1(t(j),x,y); %f1=dx/dt (from first equation) % I think the problem with dimensions might be here****
keulerf2=f2(t(j),x,y); %f2=dy/dt (from second equation)
x=x+h*keulerf1; %Euler forward method for first variable
y=y+h*keulerf2; %Euler forward method for second variable
OUT=[t(j+1) x y]
endfor
endfunction
示例:
%Initial values:
t0=0;tf=3;
x0=10; %x(t0)
y0=10; %y(t0)
n=3;
OUT_0=[t0 x0 x0 y0 y0]
f1=@(t,x,y) [2*x 0]; %Equation 1 (arbitrary example): [dx/dt]=[2x 0]
f2=@(t,x,y) [2*y]; %Equation 2 (arbitrary example): [dy/dt]=[2y]
[t,x,y]=Euler(f1,f2,t0,tf,x0,y0,n)
%It only works for f1, f2 of 1x1 size. I don't quite know why the dimensions aren't being consistent.
感谢任何反馈。祝你有个愉快的一天。
如果我理解正确,你应该为你的 f1 使用矩阵注释 A*x
这是欧拉:
function [t,x,y]=Euler(f1,f2,t0,tf,x0,y0,n)
h=(tf-t0)/n; %Constant step size
t=t0:h:tf;
x=[x0]; % also define your two starting values better outside this function
y=[y0]; %Starting values
for j=1:n
keulerf1=f1(t(j),x,y); %f1=dx/dt (from first equation)
keulerf2=f2(t(j),x,y); %f2=dy/dt (from second equation)
x=x+h*keulerf1; %Euler forward method for first variable
y=y+h*keulerf2; %Euler forward method for second variable
OUT=[t(j+1) x' y']
endfor
endfunction
这将是你的职能:
%Initial values:
t0=0;tf=3;
x0=[10 10]'; %x(t0)
y0=10; %y(t0)
n=3;
OUT_0=[t0; x0; y0; y0]
f1=@(t,x,y) [2 0; 0 0]*x; %Equation 1 (arbitrary example): [dx/dt]=A*x
f2=@(t,x,y) [2*y]; %Equation 2 (arbitrary example): [dy/dt]=[2y]
[t,x,y]=Euler(f1,f2,t0,tf,x0,y0,n)