How to fit nonlinear data in a curve using the below equation?

1 次查看(过去 30 天)
Hi everyone,
I have a two vector with same demension: CpVector,MyVector :228*1
I am using the below equation as myfun and then using lsqcurvefit to fit the data into a curve. but i am getting error in matrix dimension while plotting.
x0 = [.05, 1, -.05,0.1];
lb = [0, 2, 0.5, -0.6];
ub = [1, -1.5,- 1,1];
myfun_cp = @(x,CpVector) x(1)/(1+x(2).*CpVector) + x(3)/CpVector +x(4);
X = lsqcurvefit(myfun_cp, x0, CpVector, MyVector, lb, ub);
c1=min(CpVector);
c2=max(CpVector);
times=linspace(c1,c2);
plot(Zone_N{1},Zone_N{2},'*r',Zone_A{1},Zone_A{2},'*g',...
Zone_B{1},Zone_B{2},'*black',times,myfun_cp(X,times),'b-');
%zones are just part of the data in CpVector and MyVector
Error using /
Matrix dimensions must agree.
Is the function i am using not correct for curve fitting? if the function is okay then what is the best way/function to use to get desired output.
Below diagram is the data plotted CpVector(X-axis) vs MyVector(Y-axis)
  4 个评论
Adam Danz
Adam Danz 2021-4-23
After adding my answer I see that the error message you shared is different than the error that I received when trying to fit the data with the bounds defined in your question.
Are you sure you've provided us with the correct data?
More importantly, please share the entire error message. Error messages usually contain line numbers that show what line is causing the error.
Chandrashekar D S
Chandrashekar D S 2021-4-25
below is the entire error message:
Error using /
Matrix dimensions must agree.
Error in FrictionCoefficients>@(x,CpVector)x(1)/(1+x(2).*CpVector)+x(3)/CpVector+x(4) (line 487)
myfun_cp = @(x,CpVector) x(1)/(1+x(2).*CpVector) + x(3)/CpVector +x(4);
Error in FrictionCoefficients/StartcalculationButtonPushed (line 494)
Zone_B{1},Zone_B{2},'*black',times,myfun_cp(X,times),'b-');
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2021-4-23
The error message is quite clear,
Exiting due to infeasibility: at least one lower bound exceeds the corresponding upper bound.
Looks at the bounds.
>> table(lb(:),ub(:),'VariableNames',{'LowerBound','UpperBound'})
ans =
4×2 table
LowerBound UpperBound
__________ __________
0 1
2 -1.5
0.5 -1
-0.6 1
Two of the parameter lower bounds are higher than the upper bounds!
That's like asking to pick a number greater than 100 but less than 10.
  3 个评论
Adam Danz
Adam Danz 2021-4-25
编辑:Adam Danz 2021-4-25
Based on the error message the error is this lline
myfun_cp = @(x,CpVector) x(1)/(1+x(2).*CpVector) + x(3)/CpVector +x(4);
Try replacing / with ./
For more info:

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Least Squares 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by