Can't solve ODE with pchip and ppval

Hi, I'm getting a error I can't understand and I would love if someone could clarify for me why the following doesn't work: I have two ode's, one with and one without noise. The one with works, but the one without doesn't. The one without looks like this:

function [dFTdt] = SVP(t,x,p,SVP)
alpha = p(1);
beta = p(2);
ki=p(3);
FT=x(1);
dFTdt = zeros(1,1);
dFTdt(1)=beta - ppval(SVP,t) * ki - FT * alpha;
end

and the one with like this:

function [dFTdt] = SVPnoise(t,x,p,SVP)
alpha = p(1);
beta = p(2);
ki=p(3);
FT=x(1);
dFTdt = zeros(1,1);
dFTdt(1)=beta - ppval(SVP,t) * ki - FT * alpha + normrnd(0,0.5);
end

I can solve the one with noise in the following way:

[~,yx] = ode45(@(t,x) SVPnoise(t,x,BestLSQ12,SVP12),tData,FT(1,1))

where

BestLSQ12 = [327.1456  591.7595  471.9959]; SVP(:,1) = 0.5000 0.8000 0.3000 0.7500 0.5000 0.2500 0.2300 0.4000 0.2400 0.2000 0.7000 0.3500 0.3500 1.0100]; tData = 1.0000 1.3300 1.6600 2.0000 2.3300 2.6600 3.0000 3.3300 3.6600 4.0000 4.3300 4.6600 5.0000 5.3300]; SVP12 = pchip(tData,SVP(:,1)); FT(1,1) = 0.1000; 

I solve the one without noise in the following way:

[~,yx] = ode45(@(t,x) SVP(t,x,BestLSQ12,SVP12),tData,FT(1,1))

and then I get the following error:

Function 'subsindex' is not defined for values of class 'struct'.
Error in @(t,x)SVP(t,x,BestLSQ22,SVP12)
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
    odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

I'm still a MATLAB novice, so I have no idea why this appears. I would love to know for future progress. If anyone care to write a short line about it---I would be very grateful. //Best regards

 Accepted Answer

Please check if "SVP" is treated as function handle:
which('SVP')
before the line:
[~,yx] = ode45(@(t,x) SVP(t,x,BestLSQ12,SVP12),tData,FT(1,1))
The message might mean, that "SVP" is a struct here.
If this is not the problem, use the debugger for a deeper investigation:
dbstop if error
Now Matlab stops, when the error occurs. Then check the classes of the variables
SVP, t,x,BestLSQ22,SVP12

5 Comments

Yes, indeed I had made the mistake of making a variable called SVP---thank you! Also, thank you for your other tips, can I add the 'dbstop if error' at the start of any script? Does this make it so that I don't have to click on the breakpoints manually?
dbstop if error is useful during debugging. But if the codes are used for production, a standard error message is better than offering all users the chance to insert their opposed bugfixes directly in the code. Therefore I would not include commands for controlling the debugging tools in the code.
Ahh! Thank you!
A sidenote: it does not make sense to include random variables in a usual numerical integration with ODE45.
You will have to switch to solution methods for stochastic differential equations.
Best wishes
Torsten.
Exactly, Torsten is right. I have not seen the random values. Matlab's ODE integrators handle only smooth functions correctly. With random or discontinous values, the step size control drives crazy and you get either very noisy results caused by the accumulation of rounding errors and tiny step sizes, or the integration stops. If you get a "result" it might be dominated by the rounding error, not by the noise injected in teh data.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 7 Nov 2016

Commented:

Jan
on 8 Nov 2016

Community Treasure Hunt

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

Start Hunting!