Getting error when executing code from function, not when execute from command window

1 view (last 30 days)
Hi,
I am trying to create a function that takes inputs x and t. This function will than call the solution of the nonlinear differential equation system by executing ode45 on that system.
The problem I am getting is:
If I enter the code from the function into a blank .m file but DON'T make it a function and take the variables and assign them values manually, when i execute it i get the result from ode45.
If i take the same code, change the values for variables so they are no longer numbers but rather take values from input argument of the function and attempt to call the function from command window i get the following set of errors:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> nelin_am at 6
dy(1)= oms*v_sd-oms*rs*gs*y(1)-oms*rs*gm*y(3)+oms*y(2);
Error in ==> odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in ==> nelin_am_ode at 19
[t1,yout]=ode45('nelin_am',t2,y0);
The .m file called 'Untitled5.m' is my code in non-function form.
The .m file called 'nelin_am_ode.m' is my code in fucntion form.
The .m file called 'nelin_am.m'is the nonlinear differential equations system i am trying to solve by ode45 formated with syntax that works with function ode45.
I use the following code when trying to call the function nelin_am_ode.m from command window:
>> clear all;
>> rs1=0.008; rr1=0.015; Tm1=3.4259;
>> ls=4.07; lr=4.057; lm= 4; oms=100*pi;
>> x=[rs1 rr1 ls lr lm Tm1];
>> t=0:0.0001:4;[i_abc,x,t1]=nelin_am_ode(x,t)
I am using MatLab r2009a.
Hope someone can help with this problem.
  1 Comment
Adam
Adam on 23 Oct 2015
I don't use ode45 so I'm not familiar with the type of arguments it takes, but just looking at the code I would not be at all surprised if the global variables are responsible for the errors you are seeing.
Tossing a load of global variables into a function means the function has almost no control itself over what the data is that it is using. If you need variables in a function you should just pass them in as input arguments like you are with 'x' and 't'.
It is very difficult to know what any of those global variables contain (if anything at all) at the exact moment they are being referred to in your ode function.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!