Plotting multiple polynomials in matlab

In summary, to plot multiple polynomials in MATLAB, use the plot function and specify each polynomial as a separate input. You can plot polynomials with different degrees as long as they are specified correctly. To add a legend, use the legend function with a cell array of strings representing the labels. You can also change the color and style of each polynomial using optional inputs for the plot function or the color and linestyle functions. To add a title and axis labels, use the title, xlabel, and ylabel functions after plotting the polynomials but before adding a legend.
  • #1
TaylorWatts
16
0
Ok, I can plot a single polynomial easy enough such as 3*(x^2)-1 using fplot, but I want to graph multiple polynomials.

When I try to use the plot it doesn't work even for one though. The graph is completely wrong.

ie I make a new m-file.

x = [-1:1];

y = 3*x.^2 - 1;

Then call the m-file in the command window.

Then plot(x,y,'-b')

But I get something resembling the absolute value of x instead :confused:
 
Physics news on Phys.org
  • #2
There is only three points in your x vector! try a smaller step size.
 

1. How do I plot multiple polynomials in MATLAB?

To plot multiple polynomials in MATLAB, you can use the plot function and specify each polynomial as a separate input. For example, if you have two polynomials p1 and p2, you can plot them like this: plot(x, p1, x, p2), where x is the range of values for the x-axis.

2. Can I plot polynomials with different degrees in MATLAB?

Yes, you can plot polynomials with different degrees in MATLAB. The plot function can handle polynomials of any degree, as long as they are specified correctly. Just make sure to have the same number of coefficients for each polynomial and to specify them in descending order of degree.

3. How can I add a legend to my plot of multiple polynomials in MATLAB?

To add a legend to your plot of multiple polynomials in MATLAB, you can use the legend function. This function takes in a cell array of strings as an input, where each string represents the label for a polynomial. For example, if you have two polynomials p1 and p2, you can add a legend like this: legend({'p1', 'p2'}).

4. Is it possible to change the color and style of each polynomial in the plot?

Yes, you can change the color and style of each polynomial in the plot. The plot function has optional inputs for specifying the color and style of the line, such as 'r--' for a red dashed line. You can also use the color and linestyle functions to change these properties after plotting the polynomials.

5. Can I add a title and axis labels to my plot of multiple polynomials in MATLAB?

Yes, you can add a title and axis labels to your plot of multiple polynomials in MATLAB. The title, xlabel, and ylabel functions can be used to add these elements to your plot. Make sure to include them after plotting the polynomials, but before adding a legend.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
996
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
Back
Top