Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Laboratory Activity No.

6
Curve Fitting
Name: Lahom, Section: CHE31S1
Date Performed: 06/10/21 Date Submitted: 06/12/21
Instructor:
1. Objective(s):

1.1 To find the best equations that would fit to a series of data points using MATLAB
1.2 To solve some applications for the linear and polynomial regression using MATLAB

2. Intended Learning Outcomes (ILOs):

The students shall be able to:


2.1 Demonstrate scientific thinking and the ability to approach scientific resources intelligently.
2.2 Utilize MATLAB software in finding the best equations that would fit to a series of data points.
2.3 Infer appropriate conclusions based upon the results of activity.
2.4 Reflect on personal transformation along the TIP graduate attributes, specifically, professional
competence and critical thinking skills.

3. Discussion:

Curve fitting is a technique of finding an algebraic relationship that “best” (in least squares sense) fits a
given set of data. Unfortunately, there is no magical function (in MATLAB or otherwise) that can give the
relationship if we simply supply the data. We have to have an idea of what kind of relationship might
exist between the input data and the output data. However, if we do not have the firm idea but have data
that we trust, MATLAB can help us in exploring the best possible fit.

MATLAB includes Basic Fitting in its Figure window’s Tools menu that lets us fit a polynomial curve (up
to 10th order) to the data on the fly. It also gives us options of displaying the residual at the data points
and computing the norm of the residuals. This can help in comparing different fits and then selecting the
one that best fits.
[ 0 7 23 1911 18 14 1 5 226 20 17 13 4 12 3 9 2115 24 16 10 2 8 ]
4. Procedure:

LINEAR REGRESSION

EXAMPLE 1: Fit a straight line y=ax+ b to the following data

x 1 2 3 4 5

y 1 2 4 4 6

On your command window, input x

>> x=[1 2 3 4 5]

then input y,

>> y=[1 2 4 4 6]

then type

>> polyfit(x,y,1)

Write the equation:

y=1x+0.2

2. To show the graph of the following points, On your command window, type the following

>> plot(x,y)

to rename the figure, type


>> title('linear regression')

to show the graph of the linear regression, on the figure go to “Tools” ≫ “Basic Fitting” ≫ check “linear
≫ check “show equations”
Copy the figure shown on MATLAB

EXAMPLE 2: Fit a quadratic equation y=a x2 +bx +c to the following data

x 0 0.5 1.0 1.5 2.0 2.5

y 0 0.25 1 2.25 4.0 6.25

On your command window, input x

>> x=[0 0.5 1.0 1.5 2.0 2.5]

then input y,

>> y=[0 0.25 1 2.25 4 6.25]

then type

>> polyfit(x,y,2)
Write the equation:

y=1x^2 + (1.304^15)x-8.937^-16

ans =

1.0000 0.0000 -0.0000

2. To show the graph of the following points, On your command window, type the following

>> plot(x,y)

to rename the figure, type


>> title('polynomial regression')

to show the graph of the linear regression, on the figure go to “Tools” ≫ “Basic Fitting” ≫ check
“quadratic” ≫ check “show equations”

Copy the figure shown on MATLAB


ACTIVITY

EXAMPLE 2: Fit a 5th degree polynomial functions to the following data

x -5 -1 3 7 9 13

y 10 8 6 4 5 -2

Write the equation:

y=-0.0003472x^5 + 0.005704x^4 - 0.0055456x^3 - 0.1731x^2 - 0.2085x + 7.953

ans =

-0.0003 0.0057 -0.0055 -0.1731 -0.2085 7.9531

Show the graph


EXERCISES

1. Fit a straight line y=ax+ b to the following data

x 1 2 3 3 4 4 5 6

y 0 0 0 1 1 2 2 2

Write the equation:

y=0.5000x - 0.7500

ans =

0.5000 -0.7500

Show the graph


2. Fit a quadratic equation to the following data

x 0.75 2 3 4 6 8 8.5

y 1.2 1.95 2 2.4 2.4 2.7 2.6

Write the equation:

y= -0.0307x^2 + 0.4499x + 0.9907

ans =

-0.0307 0.4499 0.9907

Show the graph


3. Fit a cubic equation to the following data

x 3 4 5 7 8 9 11 12

y 1.6 3.6 4.4 3.4 2.2 2.8 3.8 4.6

Write the equation:

y=0.0467x^3 - 1.0412x^2 + 7.1438x -11.4887

ans =

0.0467 -1.0412 7.1438 -11.4887

Show the graph


4. Fit a cubic equation to the following data

x 3 4 5 7 8 9 11 12

y 1.6 3.6 4.4 3.4 2.2 2.8 3.8 4.6

Write the equation:

y=0.0467x^3 - 1.0412x^2 + 7.1438x -11.4887

ans =

0.0467 -1.0412 7.1438 -11.4887

Show the graph:


5. Given these data

x 5 10 15 20 25 30 35 40 45 50

y 17 24 31 33 37 37 40 40 42 41

Use least-squares regression to fit a straight line:

y=0.4945x+20.6

ans =

0.4945 20.6000

Show the graph:


5. A hardware retailer wants to know the demand for a rechargeable power drill as a function of price.
The ordered pairs (25, 82), (30, 75), (35, 67), and (40, 55) represent the price x (in dollars) and the
corresponding monthly sales y.

a. Find the least squares regression line for the data.

y= -1.7800x + 127.6000

ans =

-1.7800 127.6000

b. Estimate the demand when the price is $32.95.

y= -1.7800(32.95) + 127.6000

= 68.949

5. Conclusion:

In conclusion, the Matlab software/application is a very useful tool to plot the processed data in a curve
or line. It helps prevent human error and gives more precise results. It also makes processing it easier
and faster rather than manually doing it by hand.
6. Assessment (Rubric for Activity Performance):

BEGINNER ACCEPTABLE PROFICIENT SCO


CRITERIA
1 2 3 RE
I. Activity Skills
Work that usually needs
have quality of work Provides work of the
Quality of to be checked/redone
and some small errors highest quality and
work by others to ensure
on the answer answers are correct
quality
Group is focused on the Group is consistently
Group is rarely focused
Focus on the task and what needs to stays focused on the
on the task and what to
task be done most of the task and what needs to
be done
time be done
Members do not Members occasionally Members always
Process
demonstrate targeted demonstrate targeted demonstrate targeted
Skills
process skills. process skills. process skills.
II. Work Habits
Time Members finish ahead
Members do not finish
Management Members finish on time of time with complete
on time with incomplete
/ Conduct of with incomplete data. data and time to revise
data.
Experiment data.
Members do not know Members have defined Members are on tasks
their tasks and have no
responsibilities most of and have defined
Cooperative
defined responsibilities.
the time. Group responsibilities at all
and
Group conflicts have to
conflicts are times. Group conflicts
Teamwork
be settled by the cooperatively managed are cooperatively
teacher. most of the time. managed at all times.
Clean and orderly
Clean and orderly
Neatness Messy workplace during workplace with
workplace at all times
and and after the occasional mess during
during and after the
Orderliness experiment. and after the
experiment.
experiment.
Ability to do Members require Members require Members do not need
independent supervision by the occasional supervision to be supervised by the
work teacher. by the teacher. teacher.
Other Comments / Observations:
TOTAL SCORE

Rating =
(Total Score / 21)

You might also like