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
2017 年 4 月 5 日
what do you get when you just plot(xgrid, modelFun(coefEsts, xgrid), 'Color', 'r');
採用された回答
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 件のコメント
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 Center および File 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!