I am confused by the error: "Error evaluating inline model function"

조회 수: 6 (최근 30일)
Jinglei
Jinglei 2022년 12월 31일
댓글: Jinglei 2023년 1월 19일
myfunc=inline('fitting(c,x)','c','x');
c0=[1.5,1.5];
c=nlinfit(x,y,myfunc,c0)
function ytotal = fitting(c,x)
xspan = x;
y0 = zeros(2,1);
[~,ye] = ode45(@eq2, xspan, y0);
ytotal = ye(:,1).*(1+0.2181) + ye(:,2) + 0.0667.*ye(:,2).*(c(1)-ye(:,1).*(1+0.2181))./(1+0.0667.*ye(:,2));
end
function dy=eq2(x,y)
global c
%y(1)=CE,y(2)=LE
dy=zeros(2,1);
dy(1)=0.9728.*(2.5-(1 + 0.2181).*y(1)-y(2)-0.0667.*y(2).*(c(1)-y(1).*(1 + 0.2181))./(1 + 0.0667.*y(2))).*(c(1)-y(1).*(1+0.2181))./(1+0.0667.*y(2))-0.5766.*y(1);
dy(2)=0.5596.*(2.5-(1+0.2181).*y(1)-y(1)-0.0667.*y(2).*(c(1)-y(1).*(1+0.2181))./(1+0.0667.*y(2))).*(c(2)-y(2)-0.0667.*y(2).*(c(1)-y(1).*(1+0.2181))./(1+0.0667.*y(2)))-0.5646.*y(2);
end
When I run the program, the error came out as below.
Error using nlinfit (line 207)
Error evaluating inline model function.
Error in youhuachuli1228 (line 65)
c=nlinfit(x,y,myfunc,c0)
Caused by:
Error using inlineeval (line 14)
Error in inline expression ==> fitting(c,x)
Undefined function 'fitting' for input arguments of type 'double'.
I think the problem is that the function 'fitting(c,x)' is in type of syms instead of 'double'. But I don't know how to fix it. Does anyone have any ideas?

채택된 답변

Walter Roberson
Walter Roberson 2022년 12월 31일
When you use inline() the expression is evaluated in the base workspace. Any function mentioned must be resolved from the base workspace. That effectively prohibits local functions and nested functions. 'fitting' would have to have its own fitting.m file instead of defined in the same file.
The function fitting is not symbolic at all.
You also failed to initialize the global variable c.
Do not use inline() unless someone imposes using it as a job requirement (for example a homework example showing how inline breaks and so should be avoided)
global is not recommended.
  댓글 수: 54
Walter Roberson
Walter Roberson 2023년 1월 18일
Then in that case, there are a couple of different ways you could improve the code:
  • use ga or particleswarm or other similar evolutionary algorithm that tries a wide variety of different positions, instead of looping trying different random positions
  • use MultiStart instead of looping trying different random positions
  • do a more detailed mathematical analysis of your equations in order to figure out boundary conditions so that you can generate random starting positions that will be valid. This might be difficult as it would require analyzing the ways in which your ode can fail.
Jinglei
Jinglei 2023년 1월 19일
Got it and thank you for your help. I will give them a try and get better results.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by