フィルターのクリア

How to add correlation factor to the plot?

31 ビュー (過去 30 日間)
Bhavick Singh
Bhavick Singh 2021 年 9 月 9 日
コメント済み: Mathieu NOE 2021 年 9 月 21 日
figure(1)
scatter(current, power);
h1 = lsline;
h1.LineWidth = 2;
h1.Color = 'k';
title('Correlation Plot - Current vs Power')
xlabel('Current');
ylabel('Power');
So I used the above code to plot a correlation plot between two variable. I just need to add the correlation factor. What do I need to do to add that?
The correlation factor is 0.99, I used the corrplot function. However, I made a separate graph and now I want to add the factor to my plot.

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 9 月 9 日
hello
see my little example below (your code does not show how you get the linear model fit data)
clc
clearvars
% dummy data
x = (0:100);
a = 1.5;
b = 0.235;
y = a*x +b + 3*randn(size(x));
% Fit a polynomial p of degree "degree" to the (x,y) data:
degree = 1;
p = polyfit(x,y,degree);
% Evaluate the fitted polynomial p and plot:
f = polyval(p,x);
eqn = poly_equation(p); % polynomial equation (string)
Rsquared = my_Rsquared_coeff(y,f); % correlation coefficient
figure(3);plot(x,y,'o',x,f,'-')
legend('data',eqn)
title(['Data fit - R squared = ' num2str(Rsquared)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Rsquared = my_Rsquared_coeff(data,data_fit)
% R2 correlation coefficient computation
% The total sum of squares
sum_of_squares = sum((data-mean(data)).^2);
% The sum of squares of residuals, also called the residual sum of squares:
sum_of_squares_of_residuals = sum((data-data_fit).^2);
% definition of the coefficient of correlation is
Rsquared = 1 - sum_of_squares_of_residuals/sum_of_squares;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function eqn = poly_equation(a_hat)
eqn = " y = "+a_hat(1);
for i = 2:(length(a_hat))
if sign(a_hat(i))>0
str = " + ";
else
str = " ";
end
if i == 2
eqn = eqn+str+a_hat(i)+"*x";
else
eqn = eqn+str+a_hat(i)+"*x^"+(i-1)+" ";
end
end
eqn = eqn+" ";
end
  4 件のコメント
Bhavick Singh
Bhavick Singh 2021 年 9 月 21 日
My apologies, I got busy with my project. Thanks a lot buddy
Mathieu NOE
Mathieu NOE 2021 年 9 月 21 日
no problem , good luck for the future !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCurve Fitting Toolbox についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by