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

조회 수: 1 (최근 30일)
Chandrashekar D S
Chandrashekar D S 2021년 4월 23일
댓글: Chandrashekar D S 2021년 4월 26일
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:
Chandrashekar D S
Chandrashekar D S 2021년 4월 26일
this was really helpful. Thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by