Help using data from a trend line in my code.

Is there a way to incorporate the data from a trend line into your Matlab code without typing it manually? I'm running a system that uses a linear fit trend line from a graph, but the trend line has to be able to change. Currently, every time it changes I'm using the Basic Fitting tool to apply a linear fit, and then copying the equation of the line into my code. This process isn't very efficient though. Is there a way to get and use the linear fit equation just using the code, without going to the graph and applying a trend line every time it changes?

답변 (2개)

Paul
Paul 2014년 1월 18일
편집: Paul 2014년 1월 18일

0 개 추천

Use polyfit to make the fit and polyval to evaluate the fit. For example:
p = polyfit(x,y,n); % get fit parameters
f = polyval(p,x); % the trend line
plot(x,y,'o',x,f,'-') %plot comparison
x is your x-data, y your y-data and n the order of the fit, in your case 1.

댓글 수: 3

Daniel Agin
Daniel Agin 2014년 1월 18일
What is the "n" in p = polyfit(x,y,n);
Paul
Paul 2014년 1월 18일
Like i said the order of the fit. 1=linear.
ravi
ravi 2014년 4월 25일
paul sir if i want to liner equation(y=mx+c) then what will add in this code?

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

Daniel Agin
Daniel Agin 2014년 1월 21일

0 개 추천

f is returning an array of numbers rather than a single equation. Is there a way to make it show an equation instead?

댓글 수: 1

f is not the function but the function values at the specified x points. the values of p describe the function since they are the polynomial parameter values. A linear polynomial consists of two parameters, the slope value and the offset. p(1) is the slope and p(2) is the offset. So your equation is
f(x) = p(1)*x + p(2)

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

카테고리

도움말 센터File Exchange에서 Polynomials에 대해 자세히 알아보기

질문:

2014년 1월 17일

댓글:

2014년 4월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by