简体   繁体   中英

Why do I keep receiving error with this little piece of code (ode45)

function dy = g2(x, y)

dy = -0.1 * y;

ym = ode45('g2', 0, 5, 4)

end

I receive the following message:

g2(0.5,4) Error using odearguments (line 83) The last entry in tspan must be different from the first entry.

Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

Error in g2 (line 9) ym = ode45('g2', 0, 5, 4);

I might add that this one works well:

function dy = g1(x, y)

dy = 3 * x ^ 2;

ym = ode45('g1', 2, 4, 0.5)

end

I am not understanding what you are trying to do, however I will give you an example.

Usually your functions are defined at the bottom and you call ODE like that:

t=linspace(0,7,1000);
initial_value_for_y = 0;
[t,y] = ode45(@myfunction, t, initial_value_for_y);

function dy = myfunction(t, y)
    dy = exp(-t);
end

so in the first line we define a vector for time using linspace. at the second line we set the initial value of our integration the third line calls ODE45 with a function handle, the time span and an initial value

the rest of the lines are for the definition of your function

My concern for now is that you question is not clear. Instead of asking "why isn't it working", tell us what you are trying to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM