Plotting Function 1/(X^3 + X^2 +1) in MATLAB

In summary, the conversation is about a simple function that the speaker wants to plot in MATLAB but is encountering an error. The function is 1/(X^3 + X^2 +1) and the error is caused by a division by zero at X=(-1.4656). The speaker suggests a solution involving setting a range and using a loop to plot the function.
  • #1
Electro
48
0
Hi there,
I have a simple function and I want to plot it in MATLAB but i get an error.
Maybe someone can help. The function is of the type 1/(X^3 + X^2 +1)
I can plot a polynomial but I have no idea about the function above.
Thnx
 
Physics news on Phys.org
  • #2
Electro said:
Hi there,
I have a simple function and I want to plot it in MATLAB but i get an error.
Maybe someone can help. The function is of the type 1/(X^3 + X^2 +1)
I can plot a polynomial but I have no idea about the function above.
Thnx
Your function is UNDEFINED (Illegal) at X=(-1.4656) because the denominator becomes 0 at this point which involves division by zero. If your graph includes this X value, that's probably the problem.


~~
 
Last edited:
  • #3
You could do the following:

1. Decide on your range xmax, xmin
2. Decide on the number of points you want to plot (say N=1,000)
3. Declare an Axis vector with the axis points and a Y = zeros(1,N) vector to receive the results of the calculation
4. Use a loop to perform the calc y = ... from xmin to xmax in steps (xmax - xmin)/N and populate the Y vector. Include a check which says if the calc gives an error, put zero in that cell.
5. Plot Y vs Axis.

Others with more Matlab experience may have a much better solution but let me know if you want me to knock together some useable code.
 

1. How do I plot the function 1/(x^3 + x^2 + 1) in MATLAB?

To plot this function in MATLAB, you can use the plot function and define the x-values for the range you want to plot. Then, use the function 1./(x.^3 + x.^2 + 1) as the y-values. Finally, use the xlabel and ylabel functions to label the axes and title function to add a title to your plot.

2. How do I change the color of my plot?

You can change the color of your plot by adding a third argument to the plot function, specifying the color you want. For example, plot(x, y, 'r') will plot the function in red. You can also use other color codes such as 'b' for blue, 'k' for black, or 'm' for magenta.

3. How do I add a legend to my plot?

To add a legend to your plot, you can use the legend function after plotting your function. You can specify the labels for each plot as separate arguments, or you can use the 'DisplayName' property in the plot function to specify the labels. For example, plot(x, y, 'b', 'DisplayName', 'Function 1'); legend('Location', 'best') will plot the function in blue and add a legend with the label "Function 1" in the best location.

4. How do I change the range of my plot?

To change the range of your plot, you can use the xlim and ylim functions. For example, xlim([-10, 10]) will change the x-axis limits to -10 and 10. You can also use xlim([xmin, xmax]) and ylim([ymin, ymax]) to specify different ranges for the x-axis and y-axis, respectively.

5. How do I save my plot as an image file?

To save your plot as an image file, you can use the saveas function. This function takes two arguments, the first being the figure handle and the second being the file name. For example, saveas(gcf, 'plot.png') will save your plot as a PNG file named "plot.png". You can also specify the file type by adding the file extension to the file name.

Similar threads

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