1

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!

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Ah great! This worked! Thanks a lot. The reason I got so confused was that my book states that the syntax for doing this should be: [t y] = ode23('funn',tspan,x0,options,p1,p2,.. . ). Thus I assumed that the 'mu' variable should be entered in the p1 slot.
maybe in the older version of matlab... books can be misleading!
Yes, that could be. The book is for MatLab 7.8, wheras I use MatLab 7.12.
1

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".

5 Comments

Thanks a lot! Appreciate your answer. I know get the ode23 call to work, but I get the same answers for the differential equation no matter what number I choose for mu. If I write ode23('funn',tspan,x0,options,3) or ode23('funn',tspan,x0,options,300), I get exactly the same answer. What could cause this?
Your function definition for function funn only permits 3 arguments: t, x and mu. In function definition there's no variable defined where fourth argument should enter in funn function, so MATLAB gives you an error. You can try calling funn directly from MATLAB console: funn(tspan,x0,options,3) and you'll get the same error (if you have your variables defined, of course). For optional arguments you need to use varargin. With varargin, you can check nargin to get the total number of arguments and then read varargin{1}, varargin{2} etc.
Thanks for the input! I see how calling 'funn' directly in the MATLAB console produces an error just like you say. However, I must admit I am quite new to this, so I still don't understand how I can set the 'mu' parameter upon calling 'ode23'. My book does not really explain this well.
Rasman's answer is correct, my answer is incorrect. I'm a bit tired and confused. You don't need varargin for optional arguments in this case, it's an alternative way to do it. Your function definition is correct.
OK. I really appreciate your input though :). Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.