필터 지우기
필터 지우기

Script runs, but can't see solution

조회 수: 2 (최근 30일)
Kelly McGuire
Kelly McGuire 2017년 4월 4일
댓글: Star Strider 2017년 4월 5일
My code is shown below. I am trying to fit my data with an equation we developed. I get a plot at the end of the run with no errors, but all I see is the data curve, no fitting curve. What could be wrong here? I've attached the figure that is produced when running this script. The data is in blue, and the fitting curve should show up in red...VarName1 and VarName2 are the x and y data points I imported.
t = VarName1;
y = VarName2;
plot(x,y,'--b');
xlabel('Time');
ylabel('Current');
modelFun = @(p,t)(-167.*((1 - (p(2) ./ p(1).*(0.0001 - ((4 .* 0.0001 / 3.1415)) .* (exp(-((1/20) .*...
(3.1415^2) .* t ./ 1000))-(1/3) .* exp(-((9/20) .* (3.1415^2) .* t ./ 1000)...
)) / 3.1415))) .* exp(-((p(1) .* (0.0001 - ((4 .* 0.0001 / 3.1415) .* (exp(-((1/20) .* (3.1415^2)...
.* t ./ 1000))-(1/3) .* exp(-((9/20) .* (3.1415^2) .* t ./ 1000))) / 3.1415)...
)) + p(2)) .* t ./ 1000)+(p(2) ./ p(1) .* (0.0001 - ((4 .* 0.0001 / 3.1415) .* (exp(-((1/20).*...
(3.1415^2) .* t ./ 1000))-(1/3) .* exp(-((9/20) .* (3.1415^2) .* t / 1000)...
)) / 3.1415)) + p(2))));
startingVals = [-296 0.0573];
coefEsts = nlinfit(t, y ,modelFun, startingVals);
xgrid = linspace(0,20,100);
line(xgrid, modelFun(coefEsts, xgrid), 'Color', 'r');
  댓글 수: 1
Joseph Cheng
Joseph Cheng 2017년 4월 5일
what do you get when you just plot(xgrid, modelFun(coefEsts, xgrid), 'Color', 'r');

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

채택된 답변

Star Strider
Star Strider 2017년 4월 5일
You defined your independent variable ‘xgrid’ to go from 0 to 20, while ‘t’ goes from 0 to 3.5E+5. When I define ‘xgrid’ to match ‘t’, the plot of your function looks like the data in the image you posted.
What did you intend to plot?
  댓글 수: 2
Kelly McGuire
Kelly McGuire 2017년 4월 5일
Thanks, that worked. One more question, how do I get the curve to fit only the data and not extrapolate beyond the curve (initially and at the end)?
Star Strider
Star Strider 2017년 4월 5일
My pleasure.
I’m not certain what values ‘t’ has, so I would use it as the independent variable. That should keep the fitted curve from extrapolating beyond the region of fit at the high end.
The beginning (near 0) you can either leave alone, or manually define the time where the curve begins to increase as the beginning, using the plot GUI ‘Data Cursor’ to find it. I don’t know enough about your data to figure a way to do that programmatically. It looks like the maximum deviations from the fitted curve are at the beginning, so you might use that as a criterion. I don’t know how well your curve fits your data to be certain that would be a reliable technique.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by