0% found this document useful (0 votes)
455 views5 pages

% Matlab Program To Fit Straight Line/first Degree Curve (Y Ax+b)

The document contains Matlab code to fit different types of curves to data using least squares regression. It includes programs to fit straight lines, parabolas, exponential curves, power curves of the form y=ax^b and y=ab^x. For each type of curve, it calculates the constants by minimizing the sum of squared residuals and outputs the equation of the fitted curve.

Uploaded by

Ravi Parkhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
455 views5 pages

% Matlab Program To Fit Straight Line/first Degree Curve (Y Ax+b)

The document contains Matlab code to fit different types of curves to data using least squares regression. It includes programs to fit straight lines, parabolas, exponential curves, power curves of the form y=ax^b and y=ab^x. For each type of curve, it calculates the constants by minimizing the sum of squared residuals and outputs the equation of the fitted curve.

Uploaded by

Ravi Parkhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

% Matlab Program to fit straight line/First degree curve (y=ax+b)

clc;
clear;
n=input('\nEnter the value of data entries n= ');
for i=1:1:n
x(i)=input('Enter the values of x= ');
y(i)=input('Enter the values of y= ');
end
sx=0;
sy=0;
sxx=0;
sxy=0;
for i=1:1:n
sx=sx+x(i);
sy=sy+y(i);
sxx=sxx+x(i)*x(i);
sxy=sxy+x(i)*y(i);
end
delta=sxx*n-sx*sx;
delta1=sxy*n-sy*sx;
delta2=sxx*sy-sx*sxy;
a=delta1/delta;
b=delta2/delta;
fprintf('\nThe values of constants a=%f and b=%3f',a,b);
fprintf('\nThe equation of straight line is y=%2.5f x + %2.5f',a,b);
% Plotting best fit curve y=ax+b using points (x, ya) and observed curve using (x, y)
for i=1:1:n
ya(i)=a*x(i)+b; %To find the calculated ya using eq. ya=ax+b as per solution
end
hold on
plot(x,y,'b*-');
plot(x,ya,'r+-');
xlabel('X');
ylabel('Y');
title('Graph of first degree curve using least square criteria');

OUTPUT

Enter the value of data entries n= 7


Enter the values of x= 1
Enter the values of y= 0.5
Enter the values of x= 2
Enter the values of y= 2.5
Enter the values of x= 3
Enter the values of y= 2
Enter the values of x= 4
Enter the values of y= 4
Enter the values of x= 5
Enter the values of y= 3.5
Enter the values of x= 6
Enter the values of y= 6
Enter the values of x= 7
Enter the values of y= 5.5

The values of constants a=0.839286 and b=0.071429


The equation of straight line is y=0.83929 x + 0.07143>>
% Matlab Program to fit second degree curve/parabola/Quadratic Equation (y=ax^2+bx+c)
clc;
clear;
n=input('\nEnter the value no. of data entries n= ');
for i=1:1:n
x(i)=input('Enter the values of x= ');
y(i)=input('Enter the values of y= ');
end
sx=0;
sy=0;
sxx=0;
sxxx=0;
sxxxx=0;
sxy=0;
sxxy=0;
for i=1:1:n
sx=sx+x(i);
sy=sy+y(i);
sxx=sxx+x(i)*x(i);
sxxx=sxxx+x(i)*x(i)*x(i);
sxxxx=sxxxx+x(i)*x(i)*x(i)*x(i);
sxy=sxy+x(i)*y(i);
sxxy=sxxy+x(i)*x(i)*y(i);
end
delta=sxxxx*(sxx*n-sx*sx)-sxxx*(sxxx*n-sxx*sx)+sxx*(sxxx*sx-sxx*sxx);
delta1=sxxy*(sxx*n-sx*sx)-sxxx*(sxy*n-sy*sx)+sxx*(sxy*sx-sy*sxx);
delta2=sxxxx*(sxy*n-sy*sx)-sxxy*(sxxx*n-sxx*sx)+sxx*(sxxx*sy-sxx*sxy);
delta3=sxxxx*(sxx*sy-sx*sxy)-sxxx*(sxxx*sy-sxx*sxy)+sxxy*(sxxx*sx-sxx*sxx);
a=delta1/delta;
b=delta2/delta;
c=delta3/delta;
fprintf('\nThe values of constants a=%f ,b=%f and c=%f',a,b,c);
fprintf('\nThe equation of second degree curve is y=%2.3f x^2 + %2.3f x +
%2.3f',a,b,c);
% Plotting best fit curve y=ax^2+bx+c using points (x,ya)& observed curve using (x, y)
for i=1:1:n
ya(i)=a*x(i)*x(i)+b*x(i)+c; %To find the calculated ya using equation
ya=ax^2+bx+c
end
hold on
plot(x,y,'b*-');
plot(x,ya,'r+-');
xlabel('X');
ylabel('Y');
title('Graph of second degree curve using least square criteria');
OUTPUT
Enter the value no. of data entries n= 7
Enter the values of x= -3
Enter the values of y= 12
Enter the values of x= -2
Enter the values of y= 4
Enter the values of x= -1
Enter the values of y= 1
Enter the values of x= 0
Enter the values of y= 2
Enter the values of x= 1
Enter the values of y= 7
Enter the values of x= 2
Enter the values of y= 15
Enter the values of x= 3
Enter the values of y= 30
The values of constants a=2.119048 ,b=2.928571 and c=1.666667
The equation of second degree curve is y=2.119 x^2 + 2.929 x + 1.667>>
% Matlab Program to fit an exponential curve a*e^bx
clc;
n=input('\nEnter the value no. of data entries n= ');
for i=1:1:n
x(i)=input('Enter the values of x= ');
y(i)=input('Enter the values of y= ');
Y(i)=log(y(i));
X(i)=x(i);
end
sx=0;
sy=0;
sxx=0;
sxy=0;
for i=1:1:n
sx=sx+X(i);
sy=sy+Y(i);
sxx=sxx+X(i)*X(i);
sxy=sxy+X(i)*Y(i);
end
delta=sxx*n-sx*sx;
delta1=sxy*n-sy*sx;
delta2=sxx*sy-sx*sxy;
a1=delta1/delta;
b1=delta2/delta;
a=exp(b1);
b=a1;
fprintf('\n The values of constants a1=%f and b1=%f',a1,b1);
fprintf('\n The values of constants a=%f and b=%f',a,b);
fprintf('\n The equation of exponential curve is y=%2.4fe^%2.4fx',a,b);
% Plotting best fit curve y=ae^bx using points (x, ya) and observed curve using (x, y)
for i=1:1:n
ya(i)=a*exp(b*x(i)); %To find the calculated ya using equation ya=aebx
end
hold on
plot(x,y,'b*-')
plot(x,ya,'r*--')
xlabel('X');
ylabel('Y');
title('Graph of exponential curve using least square criteria');

OUTPUT

Enter the value no. of data entries n= 4


Enter the values of x= 0.1
Enter the values of y= 1.832
Enter the values of x= 0.2
Enter the values of y= 2.238
Enter the values of x= 0.3
Enter the values of y= 2.733
Enter the values of x= 0.4
Enter the values of y= 3.338

The values of constants a1=1.999708 and b1=0.405514


The values of constants a=1.500073 and b=1.999708
The equation of exponential curve is y=1.5001e^1.9997x>>
% Matlab Program to fit power equation ax^b
clc;
n=input('\nEnter the value no. of data entries n= ');
for i=1:1:n
x(i)=input('Enter the values of x= ');
y(i)=input('Enter the values of y= ');
X(i)=log10(x(i));
Y(i)=log10(y(i));
end
sx=0;
sy=0;
sxx=0;
sxy=0;
for i=1:1:n
sx=sx+X(i);
sy=sy+Y(i);
sxx=sxx+X(i)*X(i);
sxy=sxy+X(i)*Y(i);
end
delta=sxx*n-sx*sx;
delta1=sxy*n-sy*sx;
delta2=sxx*sy-sx*sxy;
a1=delta1/delta;
b1=delta2/delta;
a=10^b1;
b=a1;
fprintf('\nThe values of constants a1=%f and b1=%f',a1,b1);
fprintf('\nThe values of constants a=%f and b=%f',a,b);
fprintf('\nThe power equation is y=%2.4f x^ %2.4f',a,b);
% Plotting best fit curve y=ax^b using points (x, ya) and observed curve using (x, y)
for i=1:1:n
ya(i)=a*power(x(i),b); %To find the calculated ya using equation ya= ax^b
end
hold on
plot(x,y,'b*-')
plot(x,ya,'r*--')
xlabel('X');
ylabel('Y');
title('Graph of power function y=ax^b curve using least square criteria');

OUTPUT

Enter the value no. of data entries n= 5


Enter the values of x= 1
Enter the values of y= 0.5
Enter the values of x= 2
Enter the values of y= 2
Enter the values of x= 3
Enter the values of y= 4.5
Enter the values of x= 4
Enter the values of y= 8
Enter the values of x= 5
Enter the values of y= 12.5

The values of constants a1=2.000000 and b1=-0.301030


The values of constants a=0.500000 and b=2.000000
The power equation is y=0.5000 x^ 2.0000>>
% Matlab Program to fit power equation ab^x
clc;
n=input('\nEnter the value no of data entries,n= ');
for i=1:1:n
x(i)=input('Enter the values of x= ');
y(i)=input('Enter the values of y= ');
Y(i)=log10(y(i));
X(i)=x(i);
end
sx=0;
sy=0;
sxx=0;
sxy=0;
for i=1:1:n
sx=sx+X(i);
sy=sy+Y(i);
sxx=sxx+X(i)*X(i);
sxy=sxy+X(i)*Y(i);
end
delta=sxx*n-sx*sx;
delta1=sxy*n-sy*sx;
delta2=sxx*sy-sx*sxy;
a1=delta1/delta;
b1=delta2/delta;
a=10^a1;
b=10^b1;
fprintf('\nThe values of constants a1=%f and b1=%f',a1,b1);
fprintf('\nThe values of constants a=%f and b=%f',a,b);
fprintf('\nThe power equation is y=%2.4f * %2.4f ^x',a,b);
% Plotting best fit curve y=ab^x using points (x, ya) and observed curve using (x, y)
for i=1:1:n
ya(i)=a*power(b,x(i)); %To find the calculated ya using equation ya=ab^x
end
hold on
plot(x,y,'b*-')
plot(x,ya,'r*--')
xlabel('X');
ylabel('Y');
title('Graph of power function y=ab^x curve using least square criteria');

OUTPUT

Enter the value no of data entries,n= 4


Enter the values of x= 1
Enter the values of y= 4
Enter the values of x= 2
Enter the values of y= 11
Enter the values of x= 3
Enter the values of y= 35
Enter the values of x= 4
Enter the values of y= 100

The values of constants a1=0.469650 and b1=0.122756


The values of constants a=2.948829 and b=1.326650
The power equation is y=2.9488 * 1.3266 ^x>>

You might also like