Two curves in one plot with wrong scaling

I want to show the measurement results of two measurements with a fitting curve in a plot. Now I want both values to start at x-value 1 and end at 13. I tried to do this with xticks and xticklabels, which has worked wonderfully so far. By adding the spline (from the curvefitting toolbox) I can no longer do it with the second value (y2).
Does anyone have an idea where the failure lies?
Here is the sample code:
y1= [ 1 2 3 4 5 6 7 8 9 10 11 12 13]
y2= [ 10 20 30 40 50 60 70 80 90 100 110 120 130]
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
f = figure(1);
set(f, 'Units', 'normalized', 'Position', [0.1, 0.1, 0.7, 0.7]);
opts.SmoothingParam = 1;
[xData, yData] = prepareCurveData( [], y1 )
[fitresult, gof] = fit( xData, yData, ft, opts );
plot(fitresult,'b')
hold on
[xData, yData] = prepareCurveData( [], y2 )
[fitresult, gof] = fit( xData, yData, ft, opts );
plot(fitresult,'r')
grid on
legend('y1','y2')
xticks([0 1 2 3 4 5 6 7 8 9 10 11 12 13 14])
xticklabels({'',1,2,3,4,5,6,7,8,9,10,11,12,13,0})

 採用された回答

darova
darova 2020 年 2 月 23 日

0 投票

Use function that fit() returns
[fitresult, gof] = fit( xData, yData, ft, opts );
% plot(fitresult,'r')
plot(xData,fitresult(xData),'r')

5 件のコメント

Mepe
Mepe 2020 年 2 月 24 日
Many thanks for your response!
The scaling now fits perfectly!
Unfortunately the curve smoothing no longer works (cou can't see it with my trivial example). Do you have an idea why?
darova
darova 2020 年 2 月 24 日
You can manually enable this feature. Try:
plot(x,y,'linesmoothing','on')
Mepe
Mepe 2020 年 2 月 24 日
Sorry, I do not understand that. Shouldn't the smoothed lines already be called up via "fitresult"?
The Code
plot(xData,fitresult(xData),'b','linesmoothing','on')
also only delivers the original data...
darova
darova 2020 年 2 月 24 日
Can you please specify what do you mean by "smooth"?
darova
darova 2020 年 2 月 25 日
What about manual spline?
[fitresult, gof] = fit( xData, yData, ft, opts );
% plot(fitresult,'r')
x1 = linspace(xData(1),xData(end),100);
y1 = spline(xData,fitresult(xData),x1);
plot(xData,fitresult(xData),'.r')
hold on
plot(x1,y1,'b')
hold off

サインインしてコメントする。

その他の回答 (1 件)

Mepe
Mepe 2020 年 2 月 25 日

0 投票

I generated a spline code using the curve fitting toolbox:
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 1;
[xData, yData] = prepareCurveData( [], y1 )
[fitresult, gof] = fit( xData, yData, ft, opts );
plot(fitresult,'b')

カテゴリ

ヘルプ センター および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

質問済み:

2020 年 2 月 23 日

コメント済み:

2020 年 2 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by