Clear Filters
Clear Filters

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

1 view (last 30 days)
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 Comments
Adam Danz
Adam Danz on 23 Apr 2021
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 on 25 Apr 2021
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.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 23 Apr 2021
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 Comments
Adam Danz
Adam Danz on 25 Apr 2021
Edited: Adam Danz on 25 Apr 2021
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:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!