I have x and y data. I need interpolate (fit) data by y = a*x + b

답변 (1개)

Star Strider
Star Strider 2021년 2월 27일

0 개 추천

To set the start point at the origin (0,0), calculate the slope as:
slope = x(:) \ y(:);
for example:
x = 0:0.1:10;
y = rand(size(x));
slope = x(:) \ y(:);
figure
plot(x, y, '.b')
hold on
plot(x, slope*x, '--r')
hold off
grid
xlabel('x')
ylabel('y')
.

댓글 수: 6

Jaroslav Zadnik
Jaroslav Zadnik 2021년 2월 27일
thank you, but I want to also know parametr a from y = ax+b
My pleasure!
That is the ‘slope’ variable in my code example.
Another way of writing that would be:
a = x(:) \ y(:);
producing the same result, this time assigning it to ‘a’.
Jaroslav Zadnik
Jaroslav Zadnik 2021년 2월 27일
Thank you!
I have some other troubles:
1) For lower curves is slope NaN
2) Where the equation y = ax+b is hidden ?
Star Strider
Star Strider 2021년 2월 27일
My pleasure!
1) The slope would be NaN only if both the ‘x’ and ‘y’ variables are vectors of zeros. Otherwise, the slope would be between 0 and ±Inf.
2) I do not understand. If you want to display the linear equation, use the text function. (I can help with that, however I need more information as to what you want to do.)
.
Jaroslav Zadnik
Jaroslav Zadnik 2021년 2월 27일
I would like to write it like this:
F1 = fit(x(1,:)',y','0.5+0.5*cos(2*pi*x/96-delta)','lower',[0],'upper',[2*pi],'startpoint',[pi]);
where I can write the equation directly
Star Strider
Star Strider 2021년 2월 27일
I do not have the Curve Fitting Toolbox (since I do not need it for what I do in my research). This requirement was not implied or stated in the original Question you posted.
Without your data, I cannot code a similar function using the Statistics and Machine Learning Toolbox or the Optimization Toolbox functions, both of which I have and frequently use. I would need more information to provide an appropriate response.

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

카테고리

도움말 센터File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

질문:

2021년 2월 27일

댓글:

2021년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by