Script runs, but can't see solution

1 visualizzazione (ultimi 30 giorni)
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 Commento
Joseph Cheng
Joseph Cheng il 5 Apr 2017
what do you get when you just plot(xgrid, modelFun(coefEsts, xgrid), 'Color', 'r');

Accedi per commentare.

Risposta accettata

Star Strider
Star Strider il 5 Apr 2017
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 Commenti
Kelly McGuire
Kelly McGuire il 5 Apr 2017
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 il 5 Apr 2017
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by