Fitting Data to a Square root Cruve

I'm looking to fit some data that I have to a function in the form of
y=k(sqrt(x))
with "k" being some constant. How would I find such a line of best fit for the data that I have?

 Respuesta aceptada

Matt J
Matt J el 6 de Sept. de 2022
k=sqrt(x(:))\y(:)

5 comentarios

Bob
Bob el 6 de Sept. de 2022
spacing=[0.5,1,1.5,2,2.5,3]
velocity=[55.71428571,72.97297297,88.46153846,100,115.1162791,116.3265306]
k=sqrt(spacing(:))\velocity(:);
scatter(spacing, velocity, 'red', 'v');
hold on;
y=k*(sqrt(spacing));
plot(y);
This is the code I'm using to plot the function, but the line is totally off of the points.
Matt J
Matt J el 6 de Sept. de 2022
Editada: Matt J el 6 de Sept. de 2022
Your plotting code neglected to plot the fit as a function of 'spacing'. It shoudl be,
spacing=[0.5,1,1.5,2,2.5,3];
velocity=[55.71428571,72.97297297,88.46153846,100,115.1162791,116.3265306];
k=sqrt(spacing(:))\velocity(:);
plot(spacing, velocity, 'rv', spacing,k*(sqrt(spacing)),'b-') ;
Bob
Bob el 6 de Sept. de 2022
thank you so much! by any chance, is there a quick way to find the r^2 value for this data trend? if you have the time that is.
Matt J
Matt J el 6 de Sept. de 2022
Editada: Matt J el 6 de Sept. de 2022
R2 = 1 - mean( (k*(sqrt(spacing)) - velocity).^2 )/var(velocity,1)
fontSize = 18;
spacing=[0.5,1,1.5,2,2.5,3];
velocity=[55.71428571,72.97297297,88.46153846,100,115.1162791,116.3265306];
coefficients = sqrt(spacing(:))\velocity(:);
fittedVelocity = coefficients*(sqrt(spacing))
fittedVelocity = 1×6
50.2134 71.0125 86.9722 100.4269 112.2806 122.9973
subplot(2, 1, 1);
plot(spacing, velocity, 'rv', spacing, fittedVelocity,'b.-') ;
grid on;
title('Velocity Measurements', 'FontSize', fontSize)
xlabel('Spacing', 'FontSize', fontSize)
ylabel('Velocity', 'FontSize', fontSize)
legend('Original Data', 'Fitted Curve', 'Location', 'northwest')
% Determine and say how well we did with our predictions, numerically, using several metrics like RMSE and MAE.
% Fit a linear model between predicted and true so we can get the R squared.
subplot(2, 1, 2);
% Draw 45 degree line.
line([50, 120], [50, 120], 'Color', 'g', 'LineWidth', 2)
hold on;
plot(velocity, fittedVelocity, 'b.-', 'LineWidth', 2, 'MarkerSize', 25);
grid on;
xlabel('Velocity (Original Data)', 'FontSize', fontSize)
ylabel('Fitted Velocity', 'FontSize', fontSize)
mdl = fitlm(velocity, fittedVelocity);
rSquared = mdl.Rsquared.Ordinary;
caption = sprintf('R Squared = %.3f', rSquared);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none')
legend('Perfect Fit Line', 'Fitted vs. Original Data', 'Location', 'northwest')

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.

Productos

Versión

R2022a

Etiquetas

Preguntada:

Bob
el 6 de Sept. de 2022

Comentada:

el 6 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by