필터 지우기
필터 지우기

Least square fit.

조회 수: 2 (최근 30일)
yahya aktas
yahya aktas 2012년 4월 20일
a) Create an m-file that requests 5 arbitrary pairs of x and y values.
You should read one pair at a time and make a plot of these with (*) and perform a least square fit. The fit should be a linear function.
The pairs should lie in the interval 0-15.
If the user tries to write negative or larger values, please remind him/her of the limitations.
b) Make a node plot of the coordinates from the assignment 2a. The connection should be from point to point according to 1->2->3->4->5->1. Add yourself a
suitable scale on the x and y axis. Give it a title also .
The points should be marked with *. Hint: Use gplot.
my code:
i=1;
while i<=5
A(i)=input('Enter any number:'); disp(A(i))
if A(i)<0
disp('Please enter a number between 0 and 15')
else if A(i)>15
disp('Please enter a number between 0 and 15')
else if A(i)>=0 && A(i)<=15
i=i+1;
else if A(i)<=15
i=i+1;
end
end
end
end
end
X=[A(1) A(2) A(3) A(4) A(5)];
disp(X);
i=1;
while i<=5
B(i)=input('Enter any number:'); disp(B(i))
if B(i)<0
disp('Please enter a number between 0 and 15')
else if B(i)>15
disp('Please enter a number between 0 and 15')
else if B(i)>=0
i=i+1;
else if B(i)<=15
i=i+1;
end
end
end
end
end
Y=[B(1) B(2) B(3) B(4) B(5)];
disp(Y);
for k=1:5,
xy(k,:)=[A(k) B(k)];
disp(xy(k,:));
end
k=[A(1)^2 A(1) 1 ; A(2)^2 A(2) 1 ; A(3)^2 A(3) 1 ; A(4)^2 A(4) 1 ;
A(5)^2 A(5) 1];
disp(k);
x1=[A(1) ; A(2) ; A(3) ; A(4) ; A(5)];
disp(x1);
y=[B(1); B(2); B(3); B(4); B(5)];
disp(y);
a=inv(k'*k)*k'*y;
disp('a1:');
disp(a(1,1));
disp('a2:');
disp(a(2,1));
disp('a3:');
disp(a(3,1));
plot(x1,y,'r*'),grid on;
hold on
x=0:0.1:15;
t=0:0.1:15;
plot(t,a(1,1)*x.^2+a(2,1)*x+a(3,1),'-.')
title('quadratic function');
xlabel('x axis');
ylabel('y axis');
I want a graphical plot connecting these 5 pairs of data: from the first to second and from there to the third and so on.
how can I connect dots like that..
Thank you very much..

답변 (1개)

bym
bym 2012년 4월 20일
change line 81 to read
plot(x1,y,'r*-'),grid on;
by the way, a is more efficiently calculated as
a = k\y;
  댓글 수: 2
yahya aktas
yahya aktas 2012년 4월 21일
but I want a node plot of the coordinates.The connection from point to point according to 1->2->3->4->5->1.
bym
bym 2012년 4월 21일
then just add the first point to x1 and y:
x1 = [x1;A(1)];
y = [y;B(1)];

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

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by