A 8 R (ssr)
A 8 R (ssr)
8: Curve fitting
Q.1. Determine an equation that can be fitted to the data using curve fitting
x=[ 0.9 1.5 3 4 6 8 9.5 ];
y=[ 0.9 1.5 2.5 5.1 4.5 4.9 6.3];
Solution: x=[ 0.9 1.5 3 4 6 8 9.5 ];
y=[ 0.9 1.5 2.5 5.1 4.5 4.9 6.3];
p=polyfit(x,y,3)
xp=0.9:0.1:9.5;
yp=polyval(p,xp);
plot(x, y, "o", xp, yp)
xlabel(‘x’); ylabel(‘y’)
Syntax
x=[ 0.9 1.5 3 4 6 8 9.5 ];
y=[ 0.9 1.5 2.5 5.1 4.5 4.9 6.3];
p=polyfit(x,y,3);
xp=0.9:0.1:9.5;
yp=polyval(p,xp);
plot(x, y, "o", xp, yp)
xlabel('x'); ylabel('y')
AND
close all
x=[0.9 1.5 3 4 6 8 9.5];
y=[0.9 1.5 2.5 5.1 4.5 4.9 6.3];
p=polyfit(x,y,3);
figure (1);
subplot(1,2,1)
plot(x,y,'s')
xp = 0.9:0.1:9.5;
yp=polyval(p,xp);
hold on
grid on
subplot(1,2,2)
plot(xp,yp,'b')
hold on
grid on
xlable('x'); ylable('y');
AND
Explanation
• p = polyfit(x, y, 3): This line uses the polyfit function in MATLAB. polyfit finds
the coefficients of a polynomial that best fits the given data. Here, it's fitting a 3rd-
degree polynomial to the x and y data. The coefficients of the polynomial are
stored in the variable p.
• xp = 0.9:0.1:9.5;: This creates a vector xp containing x-values from 0.9 to 9.5
with a step of 0.1. This is used to generate a smoother curve for plotting.
• figure(1);: This command opens a new figure window or makes figure number
1 the active figure. If a figure 1 already exists, it will be brought to the front.
This is where the plots will be displayed.
• subplot(1, 2, 1): This command divides the figure window into a 1-by-2 grid of
subplots and selects the first subplot for plotting. The arguments mean:
o 1: 1 row of subplots
o 2: 2 columns of subplots
o 1: Select the first subplot (left one)
• hold on: This command keeps the current plot in the subplot so that subsequent
plotting commands will add to the same plot. This is important if you want to
plot both the original data and the fitted curve on the same axes.
• grid on: This turns on the grid lines in the subplot, making it easier to read
values from the plot.
• subplot(1, 2, 2): This selects the second subplot (the right one) in the 1-by-2
grid for plotting.
• hold on: Again, keeps the plot so far in the subplot and adds to it.
• grid on: Turns on the grid lines for this subplot.
• xlabel('x'); ylabel('y');: These commands label the x-axis as "x" and the y-axis
as "y". Note the correct spelling is xlabel and ylabel. It's likely that only the
second subplot will show these labels. To label both, you'd need these
commands inside each subplot section.
➢ The core idea is to find a polynomial function that approximates the relationship
between the x and y data points. polyfit does this by minimizing the sum of the
squared errors between the polynomial and the actual data points. A 3rd-degree
polynomial is chosen in this solution, but you could experiment with other degrees.
polyval then allows you to evaluate that polynomial at any x-value to get the
corresponding y-value on the fitted curve.
• t = [0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0];
• w = [6.00 4.83 3.70 3.15 2.41 1.83 1.49 1.21 0.96 0.73 0.64];
• p = polyfit(t, log(w), 1);: This is the key step. Instead of fitting to w directly, it
fits to log(w). This is because the problem specifies fitting to the "log" of the data,
suggesting an exponential relationship. It fits a first-degree polynomial (a line) to
the t values and the natural logarithm of the w values.
• m = p(1);: Extracts the first coefficient from p, which represents the slope (m) of
the line fitted to the t and log(w) data.
• b = exp(p(2));: Extracts the second coefficient from p, which represents the y-
intercept of the line fitted to the t and log(w) data. It then calculates exp() of this
value to get b. Since we fitted to log(w), b represents the coefficient in the
exponential equation.
• tm = 0:0.1:5;: Creates a vector tm for plotting a smooth curve.
• wm = b * exp(m * tm);: This constructs the exponential equation: w = b * exp(m
* t), where 'b' is the coefficient and 'm' is the rate constant. It calculates the w
values (wm) based on the fitted parameters b and m and the tm values.
• plot(t, w, 'o', tm, wm);: Plots the original data points (t, w) and the fitted
exponential curve (tm, wm).
➢ This problem assumes an exponential relationship between T and W. By taking the
logarithm of W, the exponential relationship is transformed into a linear one (log(W)
= mt + log(b)). polyfit is then used to find the best-fit line for this transformed data.
Finally, the coefficients of the line are used to determine the parameters of the
original exponential equation.
➢ Key Findings:
1. Low-degree polynomials (1st, 2nd): Too simplistic, failing to capture trends.
2. Medium-degree polynomials (3rd-4th): Strike a balance between accuracy and stability.
3. High-degree polynomials (6th-7th): Fit existing data but risk overfitting, making predictions
unreliable.
4. Recommended Choice: A 4th-degree polynomial provides a stable forecast without excessive
fluctuation.
This assignment focuses on curve fitting, which means finding the best
mathematical equation to represent a given set of data.
Here are the key learning outcomes:
1. Understanding Polynomial Curve Fitting: We'll learn how to fit
different-degree polynomials to data, using MATLAB's polyfit function.
2. Applying Logarithmic Curve Fitting: We explore how logarithmic
transformations help linearize exponential relationships for curve fitting.
3. Visualizing Data Trends: We'll gain experience in plotting and analyzing
fitted curves alongside actual data points using MATLAB functions like
plot() and xlabel().
4. Error Analysis and Model Selection: We study how moving from lower-
degree to higher-degree polynomials impacts error reduction, helping us
choose the best-fit model.
5. Predictive Modeling: The assignment includes forecasting future values
(such as COVID-19 infection rates) using polynomial models, providing
insights into predictive analytics.
6. Comparing Models for Stability and Accuracy: We assess different
polynomial degrees to understand when higher-order models lead to
overfitting.
By the end of this assignment, we should be able to confidently use
MATLAB for curve fitting, analyze trends, and make data-driven
predictions.