Polynomials in Matlab - Plotting Results

In summary, polynomials in Matlab are mathematical expressions used to represent relationships or functions in various fields. To plot polynomial results, use the plot function and customize the graph. Multiple polynomials can be plotted in the same graph using the hold on and hold off functions. The roots of a polynomial can be found using the roots function and operations such as addition, subtraction, and multiplication can be performed on polynomials using the standard arithmetic operators or built-in functions. The polyder and polyint functions can also be used to differentiate and integrate polynomials.
  • #1
ineedhelpnow
651
0
View attachment 5037

Code:
y = [0.053 1.477 1.970 3.279 4.153 4.934 5.178 5.828 6.082 6.484]; % data
coef = polyfit(x,y,3) 
X=0:.1:9;
Y=polyval(coef,X);
plot(x,y,'o',X,Y)

that's my code. did i do it right?
 

Attachments

  • 1.png
    1.png
    16.9 KB · Views: 84
Physics news on Phys.org
  • #2
You need to define your $x$ values. This should be right:

Code:
y = [0.053 1.477 1.970 3.279 4.153 4.934 5.178 5.828 6.082 6.484]; % data
x = [0 1 2 3 4 5 6 7 8 9];
coef = polyfit(x,y,3) 
X=0:.1:9;
Y=polyval(coef,X);
plot(x,y,'o',X,Y)

Output:

View attachment 5038

You can add a title, legend, and axis labels if you want, using [m]title()[/m], [m]legend()[/m], [m]xlabel()[/m], [m]ylabel()[/m], respectively.
 

Attachments

  • interpolation.PNG
    interpolation.PNG
    5.2 KB · Views: 81
  • #3
i cut that part of the code out by mistake but i have it in there. i see it's the same graph though. thanks rido!
 

FAQ: Polynomials in Matlab - Plotting Results

What are polynomials in Matlab?

Polynomials in Matlab are mathematical expressions that involve variables and coefficients, and only use operations such as addition, subtraction, and multiplication. They are commonly used to represent relationships or functions in different fields of science and engineering.

How do I plot polynomial results in Matlab?

To plot polynomial results in Matlab, you can use the plot function. First, define the polynomial expression using the variables and coefficients. Then, use the plot function to plot the polynomial over a specified range of values. You can also customize the plot by adding labels, titles, and adjusting the appearance of the graph.

Can I plot multiple polynomials in the same graph in Matlab?

Yes, you can plot multiple polynomials in the same graph in Matlab by using the hold on and hold off functions. These functions allow you to plot multiple expressions on the same graph without overwriting the previous one. You can also specify the color and style of each polynomial to differentiate them.

How can I find the roots of a polynomial in Matlab?

To find the roots of a polynomial in Matlab, you can use the roots function. This function takes the coefficients of the polynomial as input and returns the roots in a vector. If the polynomial has multiple roots, they will be listed as repeated values in the vector. You can also plot the roots on the polynomial graph to visualize their location.

Can I perform operations on polynomials in Matlab?

Yes, you can perform operations on polynomials in Matlab such as addition, subtraction, and multiplication. These operations can be done using the standard arithmetic operators or built-in functions like polyadd, polysub, and polymult. Additionally, you can also differentiate and integrate polynomials using the polyder and polyint functions respectively.

Similar threads

Replies
6
Views
2K
Replies
8
Views
859
Replies
10
Views
1K
Replies
2
Views
8K
Replies
5
Views
2K
Replies
11
Views
4K
Replies
9
Views
4K
Back
Top