How do I change the value of a parameter inside Matlab's ode45 solver

99封情书 提交于 2020-01-07 03:59:11

问题


I'm trying to solve a differential equation using ode45 in Matlab. I'm running two scripts:

function xdot=linearproblem(t,x)

global kappa mass F 

xdot(1)=-(kappa/mass)*x(2)+(F/mass)*(cos(omega1*t));
xdot(2)=x(1);

xdot=xdot';

end

Then in the second script, I have

close all
clear
clc

global kappa mass F

kappa=4;
F=2;
mass=0.5;
options=odeset('omega1',[1.4 1.5 1.6]);

[t x]=ode45(@linearproblem,0:0.005:100,[0 0],options);
a=x(8000,2);

omega1=omega1'
a=a'

I'm trying to solve the equation using three values of omega1, but it's giving me an error:

Error using odeset (line 226)
Unrecognized property name 'omega1'.

Error in frequencysweep (line 12)
options=odeset('omega1',1.4);

I tried defining omega1 as an argument: function xdot=linearproblem(t,x,omega1), but that didn't help.


回答1:


There's no parameter called omega1 in the help or documentation for odeset. That function is for setting the options for ode45 (or the other ODE solvers), not for passing your own values into the integration function.

And don't use global variables. There's no need in this case and most others. Instead use an anonymous function to pass the parameters:

@(t,x)linearproblem(t,x,kappa,mass,F)

and make sure the linearproblem function itself also takes those parameters as inputs. Maybe omega1 should be passed the same way:

@(t,x)linearproblem(t,x,omega1,kappa,mass,F)

(However, you'll get an error as the first ydot equation right-hand-side will be a vector, but xdot(1) is a single element – maybe pass omega1(i) and use a for loop around your calls to ode45?)



来源:https://stackoverflow.com/questions/20106062/how-do-i-change-the-value-of-a-parameter-inside-matlabs-ode45-solver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!