You are on page 1of 15

Curve Fitting Assignment

Course: Numerical Analysis


Course Code: KIE3005
Instructor: Dr. Wan Amirul Bin Wan Mohd Mahiyiddin

Name: Sabbir Ahmed


Matric No: KIE180713 – (17146555/1)
Group: 2
Introduction:

Curve fitting is the process of constructing a curve, or mathematical function, that has
the best fit to a series of data points, possibly subject to constraints. It can be use in various
engineering application such as estimating wireless pathloss model, machine learning, and
modelling a certain system from sensors. Practical engineering problems can be solve using
the correct mathematical modelling.

Question-1

1. Power Law Model

A power law is a functional relationship between two quantities, where a relative


change in one quantity results in a proportional relative change in the other quantity,
independent of the initial size of those quantities: one quantity varies as a power of another.

The formula for Power law model can be written as:

F=α V β
By linearizing the equation, we get,

log(F) = log (α) + β log(V)………………(i)


Comparing the equation (i) with
y = a0 + a1*x……………….(ii)

log(F) = y,
log (α) = a0,
β = a1,
log(V) = x

a) Linearized the data and calculate the coefficients (values of 𝑎1 and 𝑎0) that minimizes
the error between the model and the data. Using equation a1 a0 as below

Using Matlab simulation:

n = 100000 %data length


%Power law eqn F=alpha*(V^beta)
%Linearized the data given
y =log10(W);
x = log10(V);
%Calculate a1 power law
xi=sum(x); %summation of x data
yi=sum(y); %summation of y data
xiyi= sum(x.*y); %summation of x*y data
xi2= sum(x.^2); %summation of x squared data
a1_P = (n*xiyi-xi*yi)/(n*xi2 - xi^2)
%Calculate a0 power law
ybar = yi/n; % mean value of y
xbar = xi/n; %mean value of x
a0_P = ybar - a1_P*xbar

The values of a1 and a0 are –

Putting the values a1 and a0 into the equation (ii)

y = 1.9135 – 2.9073*x

b) Final equation of Power Law Model

%Calculate alpha beta


alpha_P = 10^a0_P
beta_P = a1_P
F = alpha_P*(V.^beta_P);

The values for Alpha and Beta are:


So, the final equation for Power Law Model is –

W =81.9449∗V −2.9073

c) The linear fit r^2 value:

%Calculate St Sr
% St is quantify spread of data about the mean prior to
regression
% Sr is quantify spread of data about the regression line
%(or curve)
St_P=0;
Sr_P=0;
for i= 1:n
St_P = St_P + (y(i)-ybar)^2;
Sr_P = Sr_P + (y(i)-a0_P-a1_P.*x(i))^2;
end
%Define linear fit r^2
r_P= sqrt((St_P-Sr_P)/St_P)
r2_P=r_P^2

From the matlab output, r^2 = 0.9830


d) Plot the graph-
2. Exponential Model:

A specific way that a quantity may increase over time. It occurs when the instantaneous
rate of change of a quantity with respect to time is proportional to the quantity itself.

The equation of Exponential model is written as:

A= αe βt
By linearizing this equation, we get

ln(A) = ln (α) + βt…….(iii)


Comparing the equation (iii) with

y = a0 + a1*x……(iv)
ln(A) = y,
ln (α) = a0,
β = a1,
t=x

a) Linearized the data and calculate the coefficients (values of 𝑎1 and 𝑎0) that
minimizes the error between the model and the data.

%Part (ii) - Exponential Model


%%%Exponential model A = alpha*e^(beta*t)
%Linearized the data given
y = log(W)/log(exp(1));
x = V;
%Calculate coefficient a1 exponential model
xi=0;
yi=0;
xiyi=0;
xi2=0;
for i=1:n %summation of each desired data
xi = xi + x(i); %summation of x data
yi = yi + y(i); %summation of y data
xiyi= xiyi + x(i)*y(i); %summation of x*y data
xi2= xi2 + (x(i))^2; %summation of x squared data
end
a1_E = (n*xiyi-xi*yi)/(n*xi2 - xi^2)
%Calculate a0 exponential model
ybar = yi/n;
xbar = xi/n;
a0_E = ybar - a1_E*xbar
The values of a0 and a1 are –

Putting the values of a0 and a1 in the (iv) equation

y = 3.9659 -0.7437*x

b) Final equation of Exponential Model

%Calculate alpha beta


alpha_E = exp(a0_E)
beta_E = a1_E
A = alpha_E.*exp(beta_E.*V);

The values of Alpha and Beta are –

So, the final equation of Exponential Model is –

W = 52.7666*e−0.7437∗V
c) The linear fir r^2 value –

%Calculate St Sr
St_E=0;
Sr_E=0;
for i= 1:n
St_E = St_E + (y(i)-ybar)^2;
Sr_E = Sr_E + (y(i)-a0_E-a1_E.*x(i))^2;
end
%Define linear fit r^2
r2_E= (St_E-Sr_E)/St_E

The output –

So, r^2 = 0.9057

d) Plot the graph


3. Saturation growth rate model

Saturation-Growth Rate model come from situations where the derivative of the
dependent variable is proportional to the square of the ratio of the dependent variable to the
independent variable.

The equation of Saturation Growth Rate model is written as:

C = αt / (β+t)

By linearizing this equation, we get

1/C = 1/α + (β / α) (1/ t)………...(v)

Comparing the equation (v) with

y = a0 + a1*x……….(vi)

1/C =y
1/α = a0
β / α = a1
1/ t = x

a) Linearized the data and calculate the coefficients (values of 𝑎1 and 𝑎0) that minimizes
the error between the model and the data.

%Part (iii) - Saturation-growth-rate Model


%Saturation-growth-rate Model C=alpha*(t/(beta+t))
%Linearized the data given
y = 1./W;
x = 1./V;
%Calculate a1 Saturation-growth-rate Model
%summation of x y x*y and x^2
xi=sum(x);
yi=sum(y);
xiyi= sum(x.*y);
xi2= sum(x.^2);
a1_S = (n*xiyi-xi*yi)/(n*xi2 - xi^2)
%Calculate a0 Saturation-growth-rate Model
ybar = yi/n; % mean of y data
xbar = xi/n; %mean of x data
a0_S = ybar - a1_S*xbar

a1 and a0 values are-


Putting the values of a0 and a1 in the (vi) equation

y = 3.8083 -5.8595*x

b) The final equation for Saturation growth rate model –

%Calculate alpha beta


alpha_S = 1/a0_S
beta_S = a1_S*alpha_S
C = alpha_S.*(V./(beta_S + V));

The output:

So, the final equation of saturation growth rate model is:

W = (0.2626*V)/(-1.5386*V)

c) The linear fit r^2 value:


%Calculate St Sr
St_S=0;
Sr_S=0;
for i= 1:n
St_S = St_S + (y(i)-ybar)^2;
Sr_S = Sr_S + (y(i)-a0_S-a1_S.*x(i))^2;
end
%Define linear fit r^2
r2_S= (St_S-Sr_S)/St_S

The output:

The linear fit r^2 value is 0.3599

d) Plot the graph


Question-2: Which one of these mathematical models is the most suitable to represent
the data?

By the definition of coefficient of determination or r^2 is a statistical measurement


that examines how differences in one variable can be explained by the difference in a second
variables, when predicting the outcome of a given event. It gives an idea how perfectly the
result of the line formed by the regression equation. The higher the coefficient, the higher
percentage of points the line passes through when the data points and lines are plotted. In one
word, whichever the coefficient is closer to 1, is a better fit than any other model. If the data
lies on a straight line then, Sr = 0 and then r^2 = 1. If the value of quantifies Sr and St are
equal then r^2 = 0, which means “No improvement over the mean”. Whereas, Sr>St then r^2
is less than zero, which means, Linear fit is worse than the mean and not possible for
unconstrained linear regression.

From above shown models, the coefficient for the Power Law model is 0.9830, which is
closer to perfect fit 1. which means the curves are better fit Power Law model comparing to
Saturation growth rate, r^2 = 0.3599 and Exponential models, r^2 = 0.9057. From the
obtained result, it is justified to say that Power Law model is suitable to represent the data.
Reference Code:

%curve fitting assignment


%Name Sabbir Ahmed
%Matric KIE180713 - 17146555/1

close all; clear all; clc;


load data_17146555 %load the data given in sharepoint
n = 100000 %data length

%Part (i) - Power Law Model


%Power law eqn F=alpha*(V^beta)
%Linearized the data given

y =log10(W);
x = log10(V);
%Calculate a1 power law
xi=sum(x); %summation of x data
yi=sum(y); %summation of y data
xiyi= sum(x.*y); %summation of x*y data
xi2= sum(x.^2); %summation of x squared data
a1_P = (n*xiyi-xi*yi)/(n*xi2 - xi^2)
%Calculate a0 power law
ybar = yi/n; % mean value of y
xbar = xi/n; %mean value of x
a0_P = ybar - a1_P*xbar
%Calculate St Sr
% St is quantify spread of data about the mean prior to regression
% Sr is quantify spread of data about the regression line
%(or curve)
St_P=0;
Sr_P=0;
for i= 1:n
St_P = St_P + (y(i)-ybar)^2;
Sr_P = Sr_P + (y(i)-a0_P-a1_P.*x(i))^2;
end
%Define linear fit r^2
r_P= sqrt((St_P-Sr_P)/St_P)
r2_P=r_P^2
%Calculate alpha beta
alpha_P = 10^a0_P
beta_P = a1_P
F = alpha_P*(V.^beta_P);
%Plot graph
figure(1)
plot(V,W,'.') %plot raw data
xlabel('V')
ylabel('W')
title('Power Law Model')
hold on
plot(V,F,'linewidth',1.5,'color','r') %plot the data in non-linear
legend('Data', 'Power Law Model Graph') %Assign the graph
caption = sprintf('W = %f * V^{%f}', alpha_P, beta_P);
text(6,120, caption, 'FontSize', 12, 'Color', 'r' ); %text color and size
%Display the equation
%%

%Part (ii) - Exponential Model


%%%Exponential model A = alpha*e^(beta*t)
%Linearized the data given
y = log(W)/log(exp(1));
x = V;
%Calculate coefficient a1 exponential model
xi=0;
yi=0;
xiyi=0;
xi2=0;
for i=1:n %summation of each desired data
xi = xi + x(i); %summation of x data
yi = yi + y(i); %summation of y data
xiyi= xiyi + x(i)*y(i); %summation of x*y data
xi2= xi2 + (x(i))^2; %summation of x squared data
end
a1_E = (n*xiyi-xi*yi)/(n*xi2 - xi^2)
%Calculate a0 exponential model
ybar = yi/n;
xbar = xi/n;
a0_E = ybar - a1_E*xbar
%Calculate St Sr
St_E=0;
Sr_E=0;
for i= 1:n
St_E = St_E + (y(i)-ybar)^2;
Sr_E = Sr_E + (y(i)-a0_E-a1_E.*x(i))^2;
end
%Define linear fit r^2
r2_E= (St_E-Sr_E)/St_E
%Calculate alpha beta
alpha_E = exp(a0_E)
beta_E = a1_E
A = alpha_E.*exp(beta_E.*V);
%Plot graph
figure(2)
plot(V,W,'.')
xlabel('V')
ylabel('W')
title('Exponential Method')
hold on
plot(V,A,'linewidth',1.5,'color','g')
legend('Data', 'Exponential Model Graph')
caption = sprintf('W = %f * e^{%f * V}', alpha_E,beta_E);
text(6,100, caption, 'FontSize', 12, 'Color', 'g' ); %text color and size
%%

%Part (iii) - Saturation-growth-rate Model


%Saturation-growth-rate Model C=alpha*(t/(beta+t))
%Linearized the data given
y = 1./W;
x = 1./V;
%Calculate a1 Saturation-growth-rate Model
%summation of x y x*y and x^2
xi=sum(x);
yi=sum(y);
xiyi= sum(x.*y);
xi2= sum(x.^2);
a1_S = (n*xiyi-xi*yi)/(n*xi2 - xi^2)
%Calculate a0 Saturation-growth-rate Model
ybar = yi/n; % mean of y data
xbar = xi/n; %mean of x data
a0_S = ybar - a1_S*xbar
%Calculate St Sr
St_S=0;
Sr_S=0;
for i= 1:n
St_S = St_S + (y(i)-ybar)^2;
Sr_S = Sr_S + (y(i)-a0_S-a1_S.*x(i))^2;
end
%Define linear fit r^2
r2_S= (St_S-Sr_S)/St_S
%Calculate alpha beta
alpha_S = 1/a0_S
beta_S = a1_S*alpha_S
C = alpha_S.*(V./(beta_S + V));
%Plot graph Saturation-growth-rate Model
figure(3)
plot(V,W,'.')
axis([1 3 -800 1200]);
xlabel('V')
ylabel('W')
title('Saturation-growth-rate Model')
hold on
plot(V,C,'linewidth',1.5,'color','r')
legend('Data', 'Saturation growth rate Model Graph')
caption = sprintf('W = %f*V/(%f+V)', alpha_S, beta_S);
text(2,600, caption, 'FontSize', 12, 'Color', 'r' ); %text color and size

You might also like