Question

I am trying to learn MatLab on my own. I get most of the concepts regarding solving ODEs, but I am a little unsure about the use of optional arguments as input. I have created the following function:

function xdot = funn(t,x,mu);
if nargin < 3 | isempty(mu)
    mu = 1;
end
xdot = t +mu*x;

In addition I have defined:

tspan = [0 2];
x0 = 0;
options = odeset('outputfcn','odeplot');

What I am unsure about is how to change the variable mu when I use the ode23 function. I understand that this should be possible through the input of optional arguments, but I can't get it to work. Say if I write:

[t y] = ode23('funn',tspan,x0,options)

Then everything computes correctly, and we automatically get mu = 1.

But if I want to change this, how do I proceed? I tried writing the following to set mu = 4:

[t y] = ode23('funn',tspan,x0,options,4)

But then I just get the following:

??? Error using ==> funn
Too many input arguments.

Error in ==> odearguments at 98
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

Error in ==> ode23 at 171
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

I did this based on how I understood the procedure as it is written in my textbook, but obviously I'm doing something wrong here. If anyone can explain how I go about changing the parameter mu, and in general how optional arguments p1, p2, ... are used with ode23 I would really appreciate it!

Was it helpful?

Solution

what you need to do is pass data in the function call using anonymous functions:

[t y] = ode23(@(t,y)funn(t,y,4),tspan,x0,options)

OTHER TIPS

I think the problem has nothing to do with ode23. The problem seems to be in that you have not listed varargin in the function definition of funn. It should be:

function xdot = funn(t,x,mu,varargin)

This way funn accepts 3 or more arguments, and you should not get error "Too many input arguments".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top