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

2 次查看(过去 30 天)
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 中查找有关 Least Squares 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by