Curve fitting is a very fundamental thing in numerical analysis. And also in engineering and data analysis, curve fitting can be a very important tool. In general, data are obtained from the system separately. To see the characteristics of a bunch of data, curve fitting can be very useful.
Matlab® provides a bunch of curve fitting commands to make curve fitting from given or defined data. Here, we explain how to do curve fitting in Matlab® with vert basic examples below.
If you are interested to learn Matlab® at an engineering level, click on the given link or the ‘Shop Now’ button to check the recommended book by Mechanicalland, from Amazon!
How To Use ‘polyfit()’ Command In Matlab®?
To understand how to make curve fitting in Matlab® with polyfit() command, take a look at the example below that done in Matlab® command window.
>> a = [20, 30, 40, 60, 80, 100]; b = [1, 2, 3, 5, 8, 11]; x = polyfit(a, b, 1) x = 0.1242 -1.8316
To make the curve fitting, you need to define the variables and data. In Matlab®, you can input the two different data sets that depend on each other as vectors. We did this by defining vector ‘a’ and vector ‘b’ as you see above.
To use these vectors inside the polyfit() command, the number of elements of vectors must be the same.
As you see above, we just typed the vectors inside the polyfit() command and we assigned the polyfit() command to variable ‘x’ to see the answer in the command window.
The appeared answer above the command window includes the coefficients of the obtained curve equation. In here, the equation is 0.1242x-1.8316.
You can graph this equation by using graphing methods in Matlab®.
But in general, curve must be fit in second and third degree equations. Take a look at the example below.
>> a = [10, 100, 1000, 60, 80, 100]; b = [1, 10, 110, 120, 1150, 1100]; x = polyfit(a, b, 2) x = -0.0086 8.7737 -86.2636 >>
We defined two vectors again in Matlab® to use in the ‘polyfit()’ command. Sometimes, you would need higher-order curves instead of the first-order or linear ones.
Inside the brackets of polyfit() command above, we typed ‘1’ and ‘2’ in code examples respectively. This number is about the order of the curve.
YOU CAN LEARN Matlab® IN MECHANICAL BASE; Click And Start To Learn Matlab®!
In the first example, we obtained the first-order curve. But in the second one, we want to obtain the second-order curve. If you take a look at the answer above, the number of coefficients is three. This means that we obtained the second-order curve equation.
Here, the obtained curve is; -0.0086x^2+8.7737x-86.2636.
This is the general use of the polyfit() command in Matlab®. If you want further examples about the polyfit() command in Matlab®, inform us in the comments below.
Do not forget to leave your comments and questions below about the curve fitting in Matlab® with the polyfit() command.
Your precious feedbacks are very important to us.
Leave a Reply