To curve fit the selected data from the plot

1 次查看(过去 30 天)
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.

回答(1 个)

Nipun
Nipun 2024-6-6
编辑:Nipun 2024-6-6
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

类别

Help CenterFile Exchange 中查找有关 Curve Fitting Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by