From: Luca Turchet on 8 May 2010 22:13 Hi all, I ask you a syntax help because I have a problem in passing parameters in aa function handle whithin ode45. I define a function in the file distance_integral.m: function [dxds] = distance_integral(s,x,mu,sigma) dxds = 1/sqrt(1 + gaussian_der(x, mu, sigma).^2); Then I use such function within ODE: [S X] = ode45(@(s,x) distance_integral(s,x,mu,sigma),Sspan,IC) ....but I get this error: ??? Attempted to access distance_integral(0,1.5,0,2); index must be a positive integer or logical. Error in ==> @(s,x)distance_integral(s,x,mu,sigma) 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 ==> John_method at 68 [S X] = ode45(@(s,x) distance_integral(s,x,mu,sigma),Sspan,IC) % Solve ODE I think the error is due to the a syntax problem, because if I don´t pass the values of mu and sigma which will be used in another function (i.e. gaussian_der) I don´t get problems. Any suggestion? Thanks in advance Cheers
From: EE Student on 8 May 2010 22:46 "Luca Turchet" <tur(a)imi.aau.dk> wrote in message <hs55ng$rer$1(a)fred.mathworks.com>... > Hi all, > I ask you a syntax help because I have a problem in passing parameters in aa function handle whithin ode45. > > > > I define a function in the file distance_integral.m: > > function [dxds] = distance_integral(s,x,mu,sigma) > > dxds = 1/sqrt(1 + gaussian_der(x, mu, sigma).^2); > > > > > > Then I use such function within ODE: > > [S X] = ode45(@(s,x) distance_integral(s,x,mu,sigma),Sspan,IC) > > ...but I get this error: > > ??? Attempted to access distance_integral(0,1.5,0,2); index must be a positive integer or logical. > > Error in ==> @(s,x)distance_integral(s,x,mu,sigma) > > > 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 ==> John_method at 68 > [S X] = ode45(@(s,x) distance_integral(s,x,mu,sigma),Sspan,IC) % Solve ODE > > > > > > > I think the error is due to the a syntax problem, because if I don´t pass the values of mu and sigma which will be used in another function (i.e. gaussian_der) I don´t get problems. > > > Any suggestion? > > Thanks in advance > > Cheers Are the values of mu and sigma changing, it doesnt look like they are. If not why dont you define them as global or persistant variables or you could even hardcode the values in where you need them or defone them within the function that uses them rather than pass them in.
From: Luca Turchet on 9 May 2010 06:13 > > Are the values of mu and sigma changing, it doesnt look like they are. If not why dont you define them as global or persistant variables or you could even hardcode the values in where you need them or defone them within the function that uses them rather than pass them in. Hi, thanks a lot for the answer. No mu and sigma do not change. Can you please give me a quick code example to follow ? Moreover in the code I gave you I did exactly what you said "define them within the function that uses them rather than pass them in." but I do not want to do this, I need to pass such parameters. What do you mean with " you could even hardcode the values in where you need them" I don´t understand, maybe it is for my poor English. Cheers
From: Luca Turchet on 9 May 2010 07:03 Using the approach with global variables it works. Indeed I added the following code: global mu %mu global sigma %sigma mu = 0;%mu sigma = 2;%sigma and then I called the function [S X] = ode45(@(s,x) distance_integral(s,x,mu,sigma),Sspan,IC) % Solve ODE Cheers "EE Student " <np7(a)cec.wustl.edu> wrote in message <hs57lf$su8$1(a)fred.mathworks.com>... > "Luca Turchet" <tur(a)imi.aau.dk> wrote in message <hs55ng$rer$1(a)fred.mathworks.com>... > > Hi all, > > I ask you a syntax help because I have a problem in passing parameters in aa function handle whithin ode45. > > > > > > > > I define a function in the file distance_integral.m: > > > > function [dxds] = distance_integral(s,x,mu,sigma) > > > > dxds = 1/sqrt(1 + gaussian_der(x, mu, sigma).^2); > > > > > > > > > > > > Then I use such function within ODE: > > > > [S X] = ode45(@(s,x) distance_integral(s,x,mu,sigma),Sspan,IC) > > > > ...but I get this error: > > > > ??? Attempted to access distance_integral(0,1.5,0,2); index must be a positive integer or logical. > > > > Error in ==> @(s,x)distance_integral(s,x,mu,sigma) > > > > > > 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 ==> John_method at 68 > > [S X] = ode45(@(s,x) distance_integral(s,x,mu,sigma),Sspan,IC) % Solve ODE > > > > > > > > > > > > > > I think the error is due to the a syntax problem, because if I don´t pass the values of mu and sigma which will be used in another function (i.e. gaussian_der) I don´t get problems. > > > > > > Any suggestion? > > > > Thanks in advance > > > > Cheers > > > > Are the values of mu and sigma changing, it doesnt look like they are. If not why dont you define them as global or persistant variables or you could even hardcode the values in where you need them or defone them within the function that uses them rather than pass them in.
From: Bruno Luong on 9 May 2010 08:05
"Luca Turchet" <tur(a)imi.aau.dk> wrote in message <hs64p7$ist$1(a)fred.mathworks.com>... > Using the approach with global variables it works. > Indeed I added the following code: > > global mu %mu > global sigma %sigma > > mu = 0;%mu > sigma = 2;%sigma > > and then I called the function > [S X] = ode45(@(s,x) distance_integral(s,x,mu,sigma),Sspan,IC) % Solve ODE > Using GLOBAL might be quick and - I'm sorry to say - dirty. If you want to program properly, check out the link "Parameterizing Functions" when you type "DOC ODE45". Bruno |