plotting Graph in 3D help (2024)

45 views (last 30 days)

Show older comments

Fareeha on 4 Jul 2024 at 14:59

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help

Answered: Umar about 4 hours ago

Open in MATLAB Online

basic()

plotting Graph in 3D help (2)

function basic

K=0.2; n=0.3; lam=0.1;

%Defining parameters

sol1 = bvpinit(linspace(0,6,300),[0 0 0 0 0]);

sol = bvp4c(@bvp2D, @bc2D, sol1);

x = sol.x;

y = sol.y;

%%% Plotting of the velocity

figure (1)

plot(x, y(2, :) ,'linewidth', 1.5);

hold on

xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16)

ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16)

%% Residual of the boundary conditions

function residual = bc2D(y0, yinf)

residual=[y0(1); y0(2) - 1; y0(4)+n*y0(3); yinf(2)-lam; yinf(4)];

end

%% System of First Order ODEs

function yvector = bvp2D(~,y)

yy1 =(1/(1+K))*(-y(1)*y(3)+y(2)^2-K*y(5)-lam^2);

yy2 =(2/(2+K))*(-y(1)*y(5)+y(2)*y(4)+K*(2*y(4)+y(3)));

yvector = [y(2);y(3);yy1; y(5); yy2];

end

end

i want to plot this graph in 3D, how can I?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (2)

Umar on 4 Jul 2024 at 15:14

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#answer_1481291

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#answer_1481291

Hi Fareeha,

You asked, i want to plot this graph in 3D, how can I?

In order to do that, you can use plot3 function to achieve your goal. For more information on this function, please refer to

https://www.mathworks.com/help/matlab/ref/plot3.html#

Let me know if this answers your question.

12 Comments

Show 10 older commentsHide 10 older comments

Fareeha on 4 Jul 2024 at 16:44

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3202841

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3202841

Edited: Fareeha on 4 Jul 2024 at 16:46

thank you @Umar for your response but i dont understand how to use plot3 command for this particular code. As i have values for x and y but for z what values are to be used?

Umar on 4 Jul 2024 at 16:49

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3202851

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3202851

Hi Fareeha,

To modify the code for 3D plotting using plot3, you would need to have additional data representing a third dimension. For instance, if you have a third variable z that corresponds to the velocity values, you can modify the plotting section as follows.

% Assuming z represents the third dimension data z = sin(x); % Example third dimension data

figure(2) plot3(x, y(2, :), z, 'linewidth', 1.5); xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16) ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16) zlabel('z-axis Label', 'fontweight', 'bold', 'fontsize', 16)

In this modified code snippet, plot3 is used to create a 3D plot with x, y(2, :), and z representing the three dimensions. You can adjust the z values based on your specific data.Remember, when transitioning from 2D to 3D plotting, ensure that your data is appropriately structured to represent the third dimension accurately for meaningful visualization using plot3.

Fareeha on 4 Jul 2024 at 17:45

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3202941

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3202941

Edited: Fareeha on 4 Jul 2024 at 17:46

@Umar what if i don;t know what is z, then? can you help me understanding with an example using same code?

Fareeha on 4 Jul 2024 at 17:51

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3202951

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3202951

@Umar does contour command work for this?

Umar on 4 Jul 2024 at 18:21

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3202981

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3202981

Edited: Walter Roberson on 4 Jul 2024 at 18:30

Open in MATLAB Online

Hi Fareeha,

To answer your question about z in your example code, it will be set to zeros to plot the data in 3D space.

% Plotting the velocity in 3D

figure(1)

% Plotting in 3D

plot3(x, y(2, :), zeros(size(x)), 'linewidth', 1.5);

Unrecognized function or variable 'x'.

hold on

xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16)

ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16)

zlabel('Z-axis Label') % Adding Z-axis label

Also, in the provided function basic, which involves solving a boundary value problem using bvp4c, the primary focus seems to be on plotting the velocity profile rather than generating contour plots.

Here is a full version of your code by graphing plot in 3D.

function basic

K = 0.2;

n = 0.3;

lam = 0.1;

% Defining parameters

sol1 = bvpinit(linspace(0, 6, 300), [0 0 0 0 0]);

sol = bvp4c(@bvp2D, @bc2D, sol1);

x = sol.x;

y = sol.y;

% Plotting the velocity in 3D

figure(1)

% Plotting in 3D

plot3(x, y(2, :), zeros(size(x)), 'linewidth', 1.5);

hold on

xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16)

ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16)

zlabel('Z-axis Label') % Adding Z-axis label

% Residual of the boundary conditions

function residual = bc2D(y0, yinf)

residual = [y0(1); y0(2) - 1; y0(4) + n * y0(3); yinf(2) - lam; yinf(4)];

end

% System of First Order ODEs

function yvector = bvp2D(~, y)

yy1 = (1 / (1 + K)) * (-y(1) * y(3) + y(2)^2 - K * y(5) - lam^2);

yy2 = (2 / (2 + K)) * (-y(1) * y(5) + y(2) * y(4) + K * (2 * y(4) + y(3)));

yvector = [y(2); y(3); yy1; y(5); yy2];

end

end

In the updated code snippet, the plot3 function is used to create a 3D plot of the velocity by enhanceing visualization by adding a third dimension to the plot. In this case, the velocity data is plotted in 3D space, providing a more comprehensive view of the results. Additionally, a Z-axis label is added to the plot for clarity. The rest of the code remains the same, defining parameters, handling boundary conditions, and solving the system of first-order ODEs as before. Your bc2D function still calculates the residual of the boundary conditions, while the bvp2D function defines the system of ODEs to be solved using the bvp4c solver.

Hope this will help resolve your problem.

Fareeha on 5 Jul 2024 at 15:05

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3203666

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3203666

Thankyou @Umar I also want to generate contour plot. how can i adjust these values to generate contour?

Fareeha on 5 Jul 2024 at 15:09

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3203671

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3203671

@Umar i also want to use a loop for generating graph for different values of parameters. like i want to variate K from 1 to 3 for fixed values of n and lam, can you help me doing this?

Umar on 5 Jul 2024 at 16:06

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3203746

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3203746

Hi Fareeha,

To modify the existing code for generating a contour plot, we need to adjust the plotting section and include the necessary commands to create a contour plot. Below is the updated code with modifications for generating a contour plot:

function basic

K = 0.2; n = 0.3; lam = 0.1;

% Defining parameters

sol1 = bvpinit(linspace(0, 6, 300), [0 0 0 0 0]); sol = bvp4c(@bvp2D, @bc2D, sol1); x = sol.x; y = sol.y;

% Plotting the velocity in 3D figure(1)

% Plotting in 3D plot3(x, y(2, :), zeros(size(x)), 'linewidth', 1.5); hold on

xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16) ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16) zlabel('Z-axis Label') % Adding Z-axis label

% Generating Contour Plot

figure(2) contourf(x, y(2, :), 'LineWidth', 1.5); % Creating a filled contour plot colorbar; % Adding a color bar for reference xlabel('\eta', 'fontweight', 'bold', 'fontsize', 16) ylabel('f^{\prime}(\eta)', 'fontweight', 'bold', 'fontsize', 16) title('Contour Plot') % Adding a title to the plot

% Residual of the boundary conditions function residual = bc2D(y0, yinf) residual = [y0(1); y0(2) - 1; y0(4) + n * y0(3); yinf(2) - lam; yinf(4)]; end

% System of First Order ODEs function yvector = bvp2D(~, y) yy1 = (1 / (1 + K)) * (-y(1) * y(3) + y(2)^2 - K * y(5) - lam^2); yy2 = (2 / (2 + K)) * (-y(1) * y(5) + y(2) * y(4) + K * (2 * y(4) + y(3))); yvector = [y(2); y(3); yy1; y(5); yy2]; end

end

In this updated code, a new figure (Figure 2) is created to display the contour plot using the contourf function. The contourf function generates a filled contour plot based on the values of x and y(2, :). Additionally, a color bar is added to provide a reference for the contour levels.

By incorporating these modifications, the code will now produce a contour plot alongside the existing 3D plot, offering a visual representation of the data in a different format.

Umar on 5 Jul 2024 at 16:15

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3203756

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3203756

You also asked, i also want to use a loop for generating graph for different values of parameters. like i want to variate K from 1 to 3 for fixed values of n and lam, can you help me doing this?

You can incorporate the following code into your project, where loop iterates over the values of K from 1 to 3. For each iteration, the boundary value problem is solved using bvp4c with the corresponding value of K. The resulting solution is then plotted in 3D.

for K = 1:3 sol = bvp4c(@(t, y) bvp2D(t, y, K, n, lam), @bc2D, sol1); x = sol.x; y = sol.y;

 % Plotting in 3D plot3(x, y(2, :), zeros(size(x)), 'linewidth', 1.5);end

Fareeha on 6 Jul 2024 at 4:56

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3204096

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3204096

Edited: Fareeha on 6 Jul 2024 at 4:57

@Umar thank you, I executed this code and found this error:

Error using contourf (line 57)

Z must be at least a 2x2 matrix.

Error in basic(line 10)

contourf(x, y(2, :), 'LineWidth', 1.5);

Umar on 6 Jul 2024 at 15:24

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3204356

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3204356

Hi Fareeha,

To fix the error, ensure that the input matrix Z passed to the contourf function is at least a 2x2 matrix. Here's an example to demonstrate how to fix the error:

% Create sample data

x = 1:5;y = 1:5;[X, Y] = meshgrid(x, y);

% Example data, replace with your own data

Z = peaks(5);

% Plot the contour

contourf(X, Y, Z, 'LineWidth', 1.5);

Please see attached plot.

plotting Graph in 3D help (15)

In this example, X and Y are 2D matrices created using meshgrid, and Z is a sample 5x5 matrix. By ensuring that Z is a valid 2D matrix, the error should be resolved when calling contourf.

Let me know if you need further assistance or help.

Fareeha 15 minutes ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3204891

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#comment_3204891

Edited: Fareeha 3 minutes ago

  • Screenshot 2024-07-07 at 8.47.01 PM.png

@Umar thank you, i have just used z=peak(); in my code and able to generate contour.. I am attaching figure for reference.. can you tell how to check whether i am getting right figure?

can i use Z=peak(); in my code?

Sign in to comment.

Umar 39 minutes ago

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#answer_1482251

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2134516-plotting-graph-in-3d-help#answer_1482251

Hi Fareeha,

In response to your first question, to confirm if you are getting the right figure after using the "z=peak();" function in your code, you can employ various methods. One common approach is to visually inspect the output or result generated by the function to see if it aligns with your expectations. This could involve plotting or displaying the figure to ensure it matches what you anticipate. Furthermore, you can compare the output obtained from "z=peak();" with known values or expected outcomes to validate its accuracy. By verifying against established benchmarks or test cases, you can assess if the figure produced by the function is correct. Also, bear in mind that in MATLAB, the function z=peak(); is typically used to generate a sample data matrix that can be visualized as a contour plot. When you use this function, it creates a 49x49 matrix with peak values that form a peak in the center. If you want to generate the correct contour plot using this data, you do not necessarily have to modify your code. However, it is important to ensure that you are using the correct variable name when accessing the data matrix. In this case, if you initially used z=peak(); to store the data matrix, you should continue to reference it as z in your code.

Regarding your second query about using "Z=peak();" instead of "z=peak();", it typically depends on the programming language or environment you are working in. In some languages, variable names are case-sensitive, meaning that "z" and "Z" would be treated as distinct variables. Therefore, using "Z=peak();" might create a new variable "Z" rather than updating the existing variable "z". To ensure consistency and prevent potential errors, it is advisable to stick to a consistent naming convention for variables within your code. If you intend to update the original variable "z" with a new value from "peak();", it is recommended to use the same case for consistency.

Hope this answers all your questions.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and Annotation3-D Scene ControlLighting, Transparency, and Shading

Find more on Lighting, Transparency, and Shading in Help Center and File Exchange

Tags

  • bvp4c
  • 2d plotting
  • 3d plotting

Products

  • MATLAB

Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


plotting Graph in 3D help (18)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

plotting Graph in 3D help (2024)

FAQs

What is the use of 3D graph? ›

3D graphs are a type of visualization that can display data in three dimensions: x, y, and z. They can be useful for showing complex relationships, patterns, and trends in data that might not be obvious in 2D graphs.

How to plot a 3D graph in Matlab using Excel data? ›

Please follow the following steps:
  1. Prepare Your Excel File: Ensure your Excel file is organized such that it represents a grid of Z values. ...
  2. Read Data from Excel File: Use the readmatrix function to read the data from the Excel file into MATLAB.
  3. Extract X, Y, and Z Data: ...
  4. Plot the Surface:
Jun 26, 2024

What is the formula for a 3D plot? ›

3D plot are generated from data defined as Z=f(X,Y). As for 2D plots, there are two ways to obtain a 3D plot depending on the way the (X,Y,Z) values are defined: You can have your Z values in a matrix.

How do you plot a graph step by step? ›

  1. Step 1: Identify the variables. ...
  2. Step 2: Determine the variable range. ...
  3. Step 3: Determine the scale of the graph. ...
  4. Step 4: Number and label each axis and title the graph.
  5. Step 5: Determine the data points and plot on the graph. ...
  6. Step 6: Draw the graph.

How do you describe a 3D graph? ›

A 3D surface plot is a three-dimensional graph that is useful for investigating desirable response values and operating conditions. A surface plot contains the following elements: Predictors on the x- and y-axes. A continuous surface that represents the response values on the z-axis.

What is the purpose of 3D visualization? ›

3D visualization creates efficiencies within the manufacturing and product design process by providing the ability to implement AR-guided assistance during production, virtual assembly process validation, technician training, and digital factory simulation.

What is the purpose of 3D graphics? ›

3d graphics can be used for virtual reality, and augmented reality. The objects in them can be seen as any angle and not just the 2d views drawn. 3d graphics can be much more realistic then 2d.

Can Excel make 3D graphs? ›

Go to the "Insert" tab in the Excel ribbon and select the desired chart type under the "Charts" section. Choose the 3D plot option that best fits your needs. Customize your chart by adding titles and labels and adjusting the appearance of the plot. Analyze your chart to identify trends and patterns in your data.

Can Python plot 3D graphs? ›

For plotting the 3-Dimensional line graph we will use the mplot3d function from the mpl_toolkits library. For plotting lines in 3D we will have to initialize three variable points for the line equation. In our case, we will define three variables as x, y, and z.

Can you plot a 3D graph in MATLAB? ›

This example shows how to create 3-D line plots in MATLAB using the plot3 function. Create a regularly-spaced vector t from 0 to 10*pi using pi/50 as the increment between elements.

Can you plot 3D graphs in Excel? ›

First, go to the "Insert" tab in the Excel ribbon and click on the "3D Scatter" chart icon. This will bring up a list of available chart types. Select the type of 3D plot you want to create and click "OK".

How to plot an xyz graph in Excel? ›

  1. * Select the data and click on the chart wizard button:
  2. * Choose an x-y scatter graph:
  3. * Choose finish:
  4. * Click on a point on the chart to highlight the data points:
  5. * Choose Chart: Add Trendline.
  6. * Choose a linear regression and then click on the Options tab.
Mar 11, 2020

References

Top Articles
Keto Magic Cookies | The Best & Easiest Low Carb Magic Cookie Recipe
THE VERY BEST Turkey Brine Recipe | Montana Happy
Skyblock Ah History
Pga Scores Cbs
Demon Souls Moonshadestone
Orange County's diverse vegan Mexican food movement gains momentum
T800 Kenworth Fuse Box Diagram
Blowupgirls Thread
Craigslist Carpet Installers
Mashle: Magic And Muscles Gogoanime
Tmobile Ipad 10Th Gen
Sarah Lindstrom Telegram
Seacrest 7 Piece Dining Set
Strange World Showtimes Near Harkins Metrocenter 12
Old Navy Student Discount Unidays
Inloggen bij AH Sam - E-Overheid
Word Jam 1302
Pip Calculator | Myfxbook
Thor Majestic 23A Floor Plan
Cool Math Games Unblocked 76
Rocky Bfb Asset
Sitel Group®, leader mondial de l’expérience client, accélère sa transformation et devient Foundever®
Ultimate Guide to Visiting Dungeness, UK
Solar Smash Secret Achievements List 2023
Mylaheychart Login
Idaho Falls Temple Prayer Roll
Quantumonline
Eddie Scozzare Salary
Violent Night Showtimes Near Santikos Entertainment Mayan Palace
Olecranon Fractures Flower Mound
Configuring TPM 2.0 on a 6.7 ESXi host
Jvid Rina Sauce
پنل کاربری سایت همسریابی هلو
Metro By T Mobile Sign In
Holley Gamble Funeral Home In Clinton
Dki Brain Teaser
Kare11.Com Contests
4156303136
Shipstation Commercial Actress
Franco Loja Net Worth
Jodie Sweetin Breast Reduction
Snyder Funeral Homes ♥ Tending to Hearts. ♥ Family-owned ...
Swissport Timecard
Jennifer Brabson Cleek
October 31St Weather
Fgo Spirit Root
Best Drugstore Bronzers
Sparkle Nails Phillipsburg
Walmart Makes Its Fashion Week Debut
Chase Bank Time Hours
Restaurants Near Defy Trampoline Park
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 6006

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.