Polyfit in Matlab | Code with example and Introduction

polyfit in matlab
polyfit in MatLab

What is polynomial?

A polynomial is the expression of some consecutive terms, where the terms involve only arithmetical operations (addition, subtraction, multiplication). The form of the terms should be like this,

NXn

Where N is a number, X is the exponential variable and n is a positive integer. The n is also called as degree value of exponential variable X.

There are various versions of polynomial exist and some are monomial, binomial, and trinomial. An example of the polynomial has been discussed below,

Consider, x2 + 2x - 5 is a polynomial equation. f(x)= x2 + 2x - 5 is a polynomial function. Here, 1. x2 + 2. x1 – 5. x0 are similar to the form of the polynomial N. x2 + N. x1 – N. x0 .

Where, N = 1, 2, 5

X=x

and n = 2, 1, 0 (Decreasing of degree value)

Now, we can say that "polynomial is the sum of some consecutive decreasing terms".

What is Degree 'n' in a polynomial? 

The degree of a polynomial represents the highest degree among the polynomial terms. For example, from the above terms x2 + 2x - 5, we find that the value of degree n = 0, 1, 2. What is the highest number? it is 2. So the degree of x2 + 2x - 5 is n=2.

What is polyfit in MatLab?

What is the word 'polyfit'? polyfit means 'fitting by polynomial function or polynomial equation'. Polyfit is a function name in MatLab language. In MatLab, the polyfit() function finds all coefficients from a polynomial equation. The polynomial equation consists of the degree n of x and y coordinate points. The x, y coordinates should be the same size for the fitting process. Polyfit also means, 'polynomial curve fitting'.

What is polyval in MatLab?

The introduction of polyval is needed to understand the implementation of polyfit in MatLab. Polyval is a pre-defined function in MatLab language. This function receives a polynomial with its particular points. A polyval is a function in MatLab that is used to calculate the polynomial of every point of x coordinate with respect to the y coordinate axis.

What is linspace in MatLab?

linspace is also a pre-defined function in MatLab. This function is used to generate a sequence of a number to fit in a curve or to draw lines or curves. The linspace will help us to generate numbers for x and y co-ordinate.

Understanding the variable of polyfit in MatLab

If you have MatLab installed in your local operating system or in the web software, you don't need to do anything for the importing of polyfit() function. It is a pre-built function with MatLab software. Just type,

polyfit()

Assigning of polyfit in MatLab

polynomial = polyfit(x, y, n)
#returns only polynomial coefficient
[polynomial, structure] = polyfit(x, y, n)
#returns only polynomial coefficient with its structure
[polynomial, structure, mean] = polyfit(x, y, n)
#returns only polynomial coefficient, its structure, and its mean value

X -> x coordinates point of a polynomial

Y -> y coordinates point of a polynomial

n -> Degrees of a polynomial curve (equation) to be fitted

polynomial -> Coefficient of a polynomial

structure -> Structure of a polynomial to find the error using the polyval() function in MatLab.

mean -> Represent the centering scaling values of a polynomial.

The drawback of polyfit() in MatLab

One of the drawbacks of this polyfit() in MatLab is that it is not fitted with complex values. Also, it is not compatible with negative integer degree values.

Problem-solving using polyfit in Matlab

Fit the sine function using polyfit in MatLab

Code

x = linspace(0, 6*pi, 100);
y = sin(x);
p_fit = polyfit(x, y, 5);
plot(x, y);
y_fixed = polyval(p_fit, x);
line(x, y_fixed, col="green");
legend('Actual curve','Fit polynomial curve');

Output

polyfit of sine curve
polyfit in MatLab for sine

Explanation

1. Assigning, 100 points linear space between 0 to 6*pi range in x variable.

2. Then, the sine of x values is assigned in the y variable.

3. Finding the polynomial fitting curve using polyfit().

4. Plotting the x and y values in the graph figure.

5. Getting the fixed y values from the polynomial curve values and x values using the polyval() function.

6. Drawing the line of x and y fixed values in green color and fit that in the plotted graph. The green color curve is the fitted polynomial curve.

This explanation is enough to understand the below programs.

Fit the cosine function and find the error using polyfit in MatLab

Code

x = linspace(0, 6*pi, 100);
y = cos(x);
p_fit = polyfit(x, y, 5);
plot(x, y);
y_fixed = polyval(p_fit, x);
line(x, y_fixed, col="green");
line(x, y-y_fixed, col="red");
legend('Actual curve','Fit polynomial curve', 'Error');

Output

polyfit in MatLab for cosine
polyfit in MatLab for cosine with error

Explanation

Deducting y coordinate fixed values to the actual y coordinate values to find the error. The red color has been used to draw the line of error.
error = y - y_fixed

Fit the x5 – x4 + x3 - x2 + x equation curve using polyfit in MatLab

Code

x = linspace(0, pi);
y = x.^5 - x.^4 + x.^3 - x.^2 + x;
p_fit = polyfit(x, y, 2);
plot(x, y, "o");
y_fixed = polyval(p_fit, x);
line(x, y_fixed, col="green");
legend('Actual curve','Fit polynomial curve');

Output

polyfit for an equation
polyfit for an equation in MatLab

Explanation

The plotted actual curve is drawn using "o" type to make it more clearable.

Fit the x / (1 + x2equation curve and find the error using polyfit in MatLab

Code

x = linspace(0, pi);
y = x./(1+x.^x);
p_fit = polyfit(x, y, 2);
plot(x, y, "--");
y_fixed = polyval(p_fit, x);
line(x, y_fixed, col="green");
line(x, y-y_fixed, col="red");
legend('Actual curve','Fit polynomial curve', 'Error');

Output

polyfit for a set of points in MatLab
polyfit for a set of points in MatLab with error

Explanation

The red color is the error curve.

Show the error values with points in a table format

As we have all points of all coordinates (x, y, y_fixed) and error values from the (y-y_fixed), the process is easy to print all of them in a table format. There is a table() function in MatLab. Put all variables in the table() function like the below snippets.
table(x, y, y_fixed, y-y_fixed)
The output will be this,  
error printing table
All variable values in table format




Post a Comment

Previous Post Next Post