How to move fitted plot along y axis

Hey, it's me again, this time I want to shift a fit I created with cftool:
[xData, yData] = prepareCurveData( Fqr, Prem );
% Set up fittype and options.
ft = fittype( 'a*atan(2*b*t/3.83^2-t^2)', 'independent', 't', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.0496544303257421 0.902716109915281];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData,'x');
set(h,'LineWidth',2,'Markersize',7.5)
legend( h, 'Prem vs. Fqr', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'Fqr', 'Interpreter', 'none' );
ylabel( 'Prem', 'Interpreter', 'none' );
grid on
I manage to relcoate y values just by typing +pi/2 behind yData, but what about the fitted line? That's where I'm struggling.

 Accepted Answer

Why not just shift up the raw y data?
[xData, tmp] = prepareCurveData( Fqr, Prem );
yData = tmp+pi/2; % shift up your y data values
Then everything else would be the same as you had.

5 Comments

Niklas Kurz
Niklas Kurz on 24 Jun 2020
Edited: Niklas Kurz on 24 Jun 2020
well, that is just shifting the Markers (y-values), but not the line. Thing is, I tried fitting with y values +pi/2 in advance, but fit is not working. I suspect it's because of atan function.
That's why my reasoning goes like fitting with -pi/2 (bc that's working surprisingly) and add +pi/2 back on at the end of the day.
Okay, I see now the issue. Because you don't have a constant term in your regression specification, what I suggested before won't work. Before going any further, let me ask you: are you sure you don't want a constant term in your regression? Generally you'd need a compelling reason to not include one. It happens, but it's relatively uncommon.
If you did include one, you'd have something like
ft = fittype( 'c+a*atan(2*b*t/3.83^2-t^2)', 'independent', 't', 'dependent', 'y' );
where now c is the constant term. With that specification, you can then just add pi/2 to your yData as I suggested before, and it won't do anything except shift the data and fitted curve up by a constant pi/2.
If you're sure you don't want a constant term, then a slight modification of what I wrote before would work:
[xData, tmp] = prepareCurveData( Fqr, Prem );
yData = tmp+pi/2;
% Set up fittype and options.
ft = fittype( 'a*atan(2*b*t/3.83^2-t^2)+pi/2', 'independent', 't', 'dependent', 'y' );
So now pi/2 has been added to both the data and the regression specification. Everything after that would be the same as in your original post.
By adding a constant it doesn't align anymore, bc c is assigned with a weird value. Or I am just hard of understanding. However your second option just works perfectly, many thanks.
When including the estimated constant c in your regression specification, you'll get the exact same shape of fitted curve whether or not you first add pi/2 to yData, the only difference is the curve will be shifted by a constant.
However, you won't generally get the same shape of fitted curve if you're comparing "with constant c, and with pi/2 added to yData" to "without constant c, and without pi/2 added to yData". In that case, only one of your specifications involves estimating a constant, so there's no reason to think you should get the same shape of curve. Hope that clarifies.
Thank you for the clarification!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 24 Jun 2020

Commented:

on 25 Jun 2020

Community Treasure Hunt

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

Start Hunting!