How do I can build a MATLAB code to fit the following equation using least square sense ?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
here is my equation : y=a+bx;
x=[1 2 3 4 5 6];
y=[3.5 0.5 -1.5 -3 -4.5 -7 ];
I've try this code
clc;
clear all;
close all;
%we need to do a line fitting to the following equation (y=a+bx)
X=[1; 2; 3; 4; 5; 6]; %
Y=[3.5; 0.5; -1.5; -3; -4.5; -7];
N=length(X);
AB=(X'.*X)*(X'.*Y); %coefficient matrix [a,b]
plot(X,Y,'o')
hold;
plot(AB(1:3,:),AB(4:6,:),'r-');
is that right? and what is the values of a,b
Accepted Answer
Hiro Yoshino
on 23 Apr 2021

The X dependes on how you want to fit your line to the data.
e.g.,
then X will be a series of
stacked vertically upto the end of the data number.
then X will be a series of 11 Comments
Mohammad Adeeb
on 23 Apr 2021
how is that going to br reflected on my code?
is it like this
clc;
clear all;
close all;
%This program is to solve proplem in HW"8"
%we need to do a line fitting to the following equation (y=a+bx)
x=[1; 2; 3; 4; 5; 6]; %
y=[3.5; 0.5; -1.5; -3; -4.5; -7];
N=length(x);
X=[ones(N,1),x];%write x in this format to prepare for fitting process
Y=y;
AB=(X'.*X)*(X'.*Y); %coefficient matrix [a,b]
plot(X,Y,'o')
hold;
plot(AB(1)+AB(2),'r-');
Mohammad Adeeb
on 23 Apr 2021
if yes an error will happen
''Matrix dimensions must agree.
Error in HW8_least_square_Method (line 11)
phi=(X'.*X)\(X'.*Y); %coefficient matrix [a,b]''
AB = X\y;
y_hat = X*AB;
plot(x,y,'b-',x,y_hat,'r-');
Please try to see how it fits to your thought.
Mohammad Adeeb
on 23 Apr 2021

i don't think this is right
Hiro Yoshino
on 23 Apr 2021

This is what I got. It looks all OK.
x=[1; 2; 3; 4; 5; 6]; %
y=[3.5; 0.5; -1.5; -3; -4.5; -7];
N=length(x);
X=[ones(N,1),x];
b = X\y;
y_hat = X*b;
plot(x,y,'bo', x,y_hat,'r-');
This is good enough.
Mohammad Adeeb
on 23 Apr 2021
Ok Thank u so much
Mohammad Adeeb
on 23 Apr 2021
How can i get the values of a,b ?
b = X\y
is the corresponding estimated coefficient vector.
Mohammad Adeeb
on 23 Apr 2021
Thank u ❤
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
See Also
on 23 Apr 2021
on 23 Apr 2021
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)