Script runs, but can't see solution

2 次查看(过去 30 天)
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