Unrecognized function or variable

Hello!
I am trying to perform a linear interpolation within a vector of 100 points (beta) so that I can use it in the integrations. When I try to do that matlab gives me the following error: Unrecognized function or variable 'index'.
I do this as part of an optimization program using fmincon where Ti Tf and beta are the optimized variables
I have attached my code below (t is the integration time given by ODE45). Thanks to anyone who can help me!
function [B] = interp(t)
global beta Ti Tf
p = 100;
b1 = beta;
tspan = linspace(Ti,Tf,p);
dt = (Tf-Ti)/(p-1);
for i = 1:length(tspan)
if (t>=tspan(i) && t<tspan(i)+dt)
index=i;
end
end
if index<=(p-2)
if t>=tspan(index)
B = b1(index)+(b1(index+1)-b1(index))*(t-tspan(index))/dt;
else
B = b1(index);
end
else
B = b1(index);
end
end

Answers (1)

for i = 1:length(tspan)
if (t>=tspan(i) && t<tspan(i)+dt)
index=i;
end
end
That only sets index in the case that the if matched something . If it does not, then you never assign to index .
In particular, if t < Ti or t >= Tf or if Ti then you do not assign to index.
Questions:
  • why do you keep searching once you do find a match?
  • what if t is non-scalar ?
  • why are you not using interp1() ?

6 Comments

Mal
Mal on 6 Jan 2022
Edited: Mal on 6 Jan 2022
I use this function at each integration step and t is within tspan (tspan = linspace(Ti,Tf,p)) , time interval given to ODE45.
I need to do this because I need to know between which values of beta falls in on the instant of integration so that I can interpolate between the two correct beta values and then proceed to use B in the equations to integrate.
Piecewise linear interpolation is not compatible with any of the ode*() functions. The mathematics of the ode*() functions require that the functions are twice differentiable in order to get accurate answers. Piecewise linear interpolation does not have a continuous first derivative.
If you have a data table and you need interpolation, then 'spline' satisfies first and second derivative continuity... but of course spline can "ring" near sharp boundaries.
In any case... what would it hurt to add a test for t < tspan(1) or t >= tspan(end) . Maybe an
assert(t >= tspan(1) & t < tspan(end), 'time out of range')
I now have a beta value for each tspan value, so for example beta(1) corresponds to tspan(1) etc.
I'm interpolating because ODE chooses a value of t different from those contained in tspan but always within the range defined by tspan (so the if statement is always satisfied). For values of t that don't correspond to values already defined in tspan (for example if tspan(3)<t<tspan(4)) I don't have values to use for beta and for this reason I calculate them in this way.
John D'Errico
John D'Errico on 7 Jan 2022
Edited: John D'Errico on 7 Jan 2022
You never did answer why it is you are not using interp1. Never write your own code to do poorly what already exists in the form of well written code by a professional at the art.
I tryed to use it like this (I need a value of beta that corresponds to the value of t in that moment)
p = 100;
tspan = linspace(Ti,Tf,p);
B = interp1(tspan,beta,t);
but the program doesn't give me the correct answer.
That interp1() call is doing linear interpolation. As I already explained, linear interpolation is not compatible with mathematics of ode*() .

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Release

R2021b

Asked:

Mal
on 6 Jan 2022

Commented:

on 7 Jan 2022

Community Treasure Hunt

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

Start Hunting!