To curve fit the selected data from the plot

1 view (last 30 days)
DIVITA GAUTAM
DIVITA GAUTAM on 23 Apr 2021
Edited: Nipun on 6 Jun 2024
i want to linear fit the selected data from the given plot and calculate the slope of the fitted data , i first selected the data above y=-1.7 using ylim command and then applied polyfit command on to the selected data but I am just getting NaN as result and no plot.can anyone please provide a solution to this problem.

Answers (1)

Nipun
Nipun on 6 Jun 2024
Edited: Nipun on 6 Jun 2024
Hi Divita,
I understand that you want to fit a line to the selected data from your plot and calculate the slope. Here is the updated MATLAB code:
% Assuming x and y are your data vectors
x = log((T - Tc) / Tc); % Example x data
y = log(V); % Example y data
% Select data above y = -1.7
selected_indices = y > -1.7;
x_selected = x(selected_indices);
y_selected = y(selected_indices);
% Perform linear fit
p = polyfit(x_selected, y_selected, 1);
% For more information on polyfit, refer to the following MathWorks documentation: https://www.mathworks.com/help/matlab/ref/polyfit.html
% Extract slope and intercept
slope = p(1);
intercept = p(2);
% Plot original data
figure;
plot(x, y, '*');
% For more information on plot, refer to the following MathWorks documentation: https://www.mathworks.com/help/matlab/ref/plot.html
hold on;
% Plot selected data
plot(x_selected, y_selected, 'or');
% Plot the fitted line
y_fit = polyval(p, x_selected);
plot(x_selected, y_fit, '-k');
% Add labels and title
xlabel('Log((T-Tc)/Tc)');
ylabel('Log(V)');
title('Linear Fit to Selected Data');
legend('Original Data', 'Selected Data', 'Fitted Line');
% Display slope
disp(['Slope: ', num2str(slope)]);
This code will:
  1. Filter the data to include only points where "y" is greater than -1.7.
  2. Perform a linear fit on the selected data.
  3. Plot the original data, selected data, and the fitted line.
  4. Display the slope of the fitted line.
For more information on disp, refer to the following MathWorks documentation: https://www.mathworks.com/help/matlab/ref/disp.html
For more information on polyval, refer to the following MathWorks documentation: https://www.mathworks.com/help/matlab/ref/polyval.html
Hope this helps.
Regards,
Nipun

Categories

Find more on Curve Fitting Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!