How Can I Convert a 2D Plot to a 3D Plot in MATLAB?

Dear Friends, in summary, the conversation discusses converting a 2D plot in MATLAB to a 3D plot. The suggestion is to use the function plot3 and set the z component to the desired value. However, when attempting to convert a parabolic equation, an error is encountered. Further discussion and examples are provided in the conversation.
  • #1
adnan jahan
96
0
Dear Friends

I have made 2D plot in MATLAB and want to convert it in 3D can you give me any suggestions please.

if
s=x^2+y^3
x=linspace(0,2);
plot(y,s,'b')
this gives the plot of 2D
I want to make it in 3D for any value of z axis(-1 to 1)
what changes i have to do ?
 
Physics news on Phys.org
  • #2
plot3 is the function you want, set the z component equal to the value you want.
 
  • #3
not formed ,,,hmmmm
the case is,,, I have equation of two variables,,
simply
x=linspace(0,1)
y=x^2
plot(x,y,'b')
now I want to convert this parabolic in 3D
x=linspace(0,1)
z=-1:0.1:1;
y=x^2
plot3(x,y,z)
but not working it gives
? Error using ==> mpower
Matrix must be square.
 
  • #4
I tried what you said for different example work but I need graph as in attachment,
 

Attachments

  • Doc1.doc
    26.5 KB · Views: 327
Last edited:
  • #5


Hello there,

To make a 3D plot in MATLAB, you can use the "plot3" function. This function takes in three inputs - the x, y, and z coordinates of the data points. In your case, you can use the "meshgrid" function to create a grid of x and y values, and then calculate the corresponding z values using your equation s=x^2+y^3. Here is an example code:

[x,y] = meshgrid(linspace(0,2), linspace(-1,1)); % create grid of x and y values
z = x.^2 + y.^3; % calculate z values using your equation
plot3(x,y,z,'b'); % plot the 3D graph with blue color

You can also use the "surf" function to create a surface plot, or the "scatter3" function to create a scatter plot in 3D. I hope this helps! Let me know if you have any further questions.
 

Related to How Can I Convert a 2D Plot to a 3D Plot in MATLAB?

1. How do I create a 3D plot in Matlab?

To create a 3D plot in Matlab, you will need to use the "plot3" function. This function takes in three vectors as inputs, representing the x, y, and z coordinates of your data points. You can also customize the appearance of your plot by using different colors, markers, and line styles.

2. Can I plot multiple data sets in one 3D plot?

Yes, you can plot multiple data sets in one 3D plot by using the "hold on" command. This will allow you to add more data to the existing plot without clearing it. You can also use the "legend" function to label each data set.

3. How do I add a title and axis labels to my 3D plot?

To add a title and axis labels to your 3D plot, you can use the "title" and "xlabel", "ylabel", and "zlabel" functions, respectively. These functions take in a string as an argument, which will be displayed as the title or label.

4. What is the difference between a mesh plot and a surface plot in Matlab?

A mesh plot displays a wireframe representation of a surface, while a surface plot displays a colored surface with smoother edges. You can create a mesh plot using the "mesh" function and a surface plot using the "surf" function.

5. How can I save my 3D plot as an image file in Matlab?

You can save your 3D plot as an image file in Matlab by using the "saveas" function. This function takes in the plot handle and the desired file format (e.g. png, jpeg, eps) as arguments. You can also specify the resolution of the image by using the "Resolution" name-value pair.

Similar threads

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