Unrecognized function or variable

5 ビュー (過去 30 日間)
Mal
Mal 2022 年 1 月 6 日
コメント済み: Walter Roberson 2022 年 1 月 7 日
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

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 1 月 6 日
編集済み: Walter Roberson 2022 年 1 月 6 日
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 件のコメント
Mal
Mal 2022 年 1 月 7 日
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.
Walter Roberson
Walter Roberson 2022 年 1 月 7 日
That interp1() call is doing linear interpolation. As I already explained, linear interpolation is not compatible with mathematics of ode*() .

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by