linear equation without coefficients

hello. so i am a beginner in matlab and i would need some help in solving a problem. I have 2 vectors x and y with 10 values each and the equation y=a*x+b. The task is to find the values for a and b. I know how to do this on paper, but i don t know how to code it. Please help.

댓글 수: 1

Ana Monea
Ana Monea 2020년 5월 18일
Now i understand what you were saying. I was a bit confused, but now it s clear. Thank you!

답변 (1개)

William Alberg
William Alberg 2020년 5월 17일

1 개 추천

I think the polyfit command is what you are looking for. It should work like this
k = polyfit(x,y,1)
a = k(1)
b = k(2)

댓글 수: 5

Ana Monea
Ana Monea 2020년 5월 17일
thank you..i don t really understand if i should put a for also so i could find all the a and b for the 10 values from the vectors or the code that you gave me is the final answer
William Alberg
William Alberg 2020년 5월 17일
Sorry, i don't follow. do you want to find an "a" and "b" for each set of "x" and "y"?
Image Analyst
Image Analyst 2020년 5월 17일
Explain what "put a for also" means. k is the coefficients and k(1) is the coefficient of x and k(2) is the offset (what you called b). It does a linear regression through your 10 data points.
Explain what you mean when you say "all the a and b". Exactly how many a and b do you think there should be? There are only one a and one b. You need at least 2 points to pass a line through, so no matter if you have 2 points or 2000 points, you'll have only one a and one b.
Ana Monea
Ana Monea 2020년 5월 18일
I understand now what you said thank you. And also on this exercise i have 10 values for x and z vectors and i have to find the c coefficient from z=c*sqrt(x). Can you help me on that too please?
I have not tried that before. But it appears that the following code works.
F = @(x,xdata) x(1)*sqrt(xdata);
c = lsqcurvefit(F,0,x,z)
It can end up in a local minimum point, so you should be aware of that

이 질문은 마감되었습니다.

태그

질문:

2020년 5월 17일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by