如何在Matlab的ode45求解器中更改参数的值 [英] How do I change the value of a parameter inside Matlab's ode45 solver
问题描述
我正在尝试在Matlab中使用ode45
求解微分方程.我正在运行两个脚本:
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'
我正在尝试使用omega1
的三个值来求解方程,但这给了我一个错误:
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);
我尝试将omega1
定义为参数:function xdot=linearproblem(t,x,omega1)
,但这无济于事.
I tried defining omega1
as an argument: function xdot=linearproblem(t,x,omega1)
, but that didn't help.
推荐答案
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)
,并确保linearproblem
函数本身也将这些参数用作输入.也许omega1
应该以相同的方式传递:
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)
(但是,由于第一个ydot
右边的方程式是一个向量,所以会出现错误,但是xdot(1)
是单个元素–可能传递omega1(i)
并使用for
循环呼叫ode45
周围?)
(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
?)
这篇关于如何在Matlab的ode45求解器中更改参数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!