필터 지우기
필터 지우기

Data fitting in between following variables x and b for quadratic equation

조회 수: 1 (최근 30일)
clear all
clc
a=[0.450 0.486 0.546 0.589 0.633 0.656];
b=[1.5435 1.536 1.5273 1.523 1.5197 1.5179];
x=1./a.^2;
  댓글 수: 3
MOHD UWAIS
MOHD UWAIS 2023년 1월 20일
Here we can treat x as input and b as output, and coefficient may be p1, p2, ...... (say).
Torsten
Torsten 2023년 1월 20일
So you want to determine p0,p1,p2 such that (in the least-squares sense)
p0 + p1*(1/0.45)^2 + p2*(1/0.45)^4 = 1.5435
p0 + p1*(1/0.486)^2 + p2*(1/0.486)^4 = 1.536
p0 + p1*(1/0.546)^2 + p2*(1/0.546)^4 = 1.5273
...
p0 + p1*(1/0.656)^2 + p2*(1/0.656)^4 = 1.5179
?

댓글을 달려면 로그인하십시오.

채택된 답변

Mathieu NOE
Mathieu NOE 2023년 1월 23일
편집: Mathieu NOE 2023년 1월 23일
hello again
seems to me b can be expressed as a first order polynomial of x
b = 1.4952 + 0.0097108*x
you can of course increse the order if you need to.
simply change this value in the code provided below :
degree = 1;
code :
a=[0.450 0.486 0.546 0.589 0.633 0.656];
x=1./a.^2;
b=[1.5435 1.536 1.5273 1.523 1.5197 1.5179];
% Fit a polynomial p of degree "degree" to the (x,y) data:
degree = 1;
p = polyfit(x,b,degree);
% Evaluate the fitted polynomial p and plot:
f = polyval(p,x);
eqn = poly_equation(flip(p)) % polynomial equation (string)
eqn = " y = 1.4952 + 0.0097108*x "
Rsquared = my_Rsquared_coeff(b,f); % correlation coefficient
figure(1);plot(x,b,'.',x,f,'-','markersize',20)
title('b vs x')
xlabel('x')
ylabel('b')
legend('data',eqn)
title(['Data fit , R² = ' 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+" + "+a_hat(i)+"*x";
eqn = eqn+str+a_hat(i)+"*x";
else
% eqn = eqn+" + "+a_hat(i)+"*x^"+(i-1)+" ";
eqn = eqn+str+a_hat(i)+"*x^"+(i-1)+" ";
end
end
eqn = eqn+" ";
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by