MATLAB Answers

How to apply fit-function on variable

4 views (last 30 days)
Niklas Kurz
Niklas Kurz on 20 Jun 2020
Commented: Niklas Kurz on 16 Jul 2020
So I've got a fit done with cftool:
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( N, A );
% Set up fittype and options.
ft = fittype( 'poly6' );
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'A vs. N', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'N', 'Interpreter', 'none' );
ylabel( 'A', 'Interpreter', 'none' );
grid on
hold on
Now I want the fit-function to be applid on a variable, let's say y. Hence fit-function(y). For example if the fit-function would go like fit = m*x +n, I'd like to reform it to fit = m*y + n. Thanks in advance and appologies for the 3th question asked in such a short period of time.


Sign in to comment.

Accepted Answer

Aditya Patil
Aditya Patil on 16 Jul 2020
You can pass the new data to the fitobject. See the example below,
x = sin(1:10)';
x2 = cos(1:10)';
y = (1:10)';
curve = fit(x, y, 'poly2');
y2 = curve(x2);
Here y2 is calculated from the function fit on x.

  1 Comment

Niklas Kurz
Niklas Kurz on 16 Jul 2020
Thank you. I asked this question in a state of total inexperience. After dealing with Matlab daily its beauty is being more and more revealed.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!