You are on page 1of 7

Bangladesh University of Engineering & Technology (BUET), Dhaka.

Department of Electrical & Electronic Engineering.

Course No:
EEE 212.
Course Title: Numerical Technique Laboratory.
Experiment no: 4
Report on :

Curve Fitting

Date of performance: 21.03.2015


Date of Submission: 25.4.2015

Name: Sums Uz Zaman


Student ID: 1206022
Section: A1
Group No: 12
Partners ID: 1206021

Problem 1: To fit a straight line to the x and y values of the following table:
x
1
2
3
4
5
6
7

y
.05
2.5
2.0
4.0
3.5
6.0
5.5

Solution:
clear all;
close all;
x=1:7;
y=[0.5 2.5 2.0 4.0 3.5 6.0 5.5];
sum_x=0;
sum_y=0;
sum_xy=0;
sum_x_square=0;
format long;
for i=1:7
sum_x=sum_x+x(i);
sum_y=sum_y+y(i);
sum_xy=sum_xy+(x(i)*y(i));
sum_x_square=sum_x_square+(x(i)^2);
end
a1=((7*sum_xy)-(sum_x*sum_y))/((7*sum_x_square)-(sum_x)^2);
a0=mean(y)-(a1*mean(x));
disp('a0 = ');
disp(a0);
disp('a1 = ');
disp(a1);
plot(x,y,'ro');
hold on;
x1=1:0.01: 7;
y1= a0 + (a1*x1);
plot(x1,y1);

Output:
a0 = 0.071428571428571
a1 = 0.839285714285714

Output Plot:

Problem 2: To fit a second order polynomial to the data given in the following table:
x
0
1
2
3
4
5

Solution:
clear all;
close all;
x=0:5;
y=[2.1 7.7 13.6 27.2 40.9 61.1];
sum=0;
for i=1:6
sum=sum+y(i);
end
B(1,1)=sum;
x_pow(1)=6;
for i=1:4
sum1=0;
sum2=0;
for j=1:6
p1=x(j)^i;
p2=p1*y(j);
sum1=sum1+p1;
sum2=sum2+p2;
end
x_pow(i+1)=sum1;
B(i+1,1)=sum2;
end
for i=1:3
for j=1:3
if i==1 && j==1
A(1,1)=6;
else
A(i,j)=x_pow(i+j-1);
end
end
end
C=B(1:3,1);
co_eff=inv(A)*C;
disp('a0 = ');

y
2.1
7.7
13.6
27.2
40.9
61.1

disp(co_eff(1));
disp('a1 = ');
disp(co_eff(2));
disp('a2 = ');
disp(co_eff(3));
cp=[co_eff(3) co_eff(2) co_eff(1)];
x1=0:0.01:5;
y1=polyval(cp,x1);
plot(x,y,'ro');
hold on;
plot(x1,y1);

Output with plot:


a0 =
a1 =
a2 =

2.478571428571485
2.359285714285818
1.860714285714295

Problem 3: To fit the equation y=a2xb2 to the data given in the following table:
x
1
2
3
4
5

y
0.5
1.7
3.4
5.7
8.4

Solution:
clear all;
close all;
x=1:5;
y=[0.5 1.7 3.4 5.7 8.4];
sum_x=0;
sum_y=0;
sum_xy=0;
sum_x_square=0;
for i=1:5
x1(i)=log(x(i));
y1(i)=log(y(i));
sum_x=sum_x+x1(i);
sum_y=sum_y+y1(i);
sum_xy=sum_xy+(x1(i)*y1(i));
sum_x_square=sum_x_square+(x1(i)^2);
end
b2=((5*sum_xy)-(sum_x*sum_y))/((5*sum_x_square)-(sum_x)^2);
a2=exp(mean(y1)-(b2*mean(x1)));
disp('a2 = ');
disp(a2);
disp('b2 = ');
disp(b2);
x2=1:0.01:5;
y2=a2*x2.^b2;
plot(x,y,'ro');
hold on;
plot(x2,y2);

Output with plot:


a2 =

0.500933649097749

b2 =

1.751723648077360

You might also like