48 views (last 30 days)

Show older comments

Fareeha on 4 Jul 2024 at 14:59

Answered: Umar on 7 Jul 2024 at 17:34

Accepted Answer: Umar

Open in MATLAB Online

basic()

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

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

### Accepted Answer

Umar on 4 Jul 2024 at 15:14

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

Show 10 older commentsHide 10 older comments

Fareeha on 4 Jul 2024 at 16:44

#### Direct link to this comment

https://support.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://support.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://support.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://support.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://support.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://support.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://support.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://support.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://support.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://support.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://support.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.

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 on 7 Jul 2024 at 15:46

#### Direct link to this comment

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

Edited: Fareeha on 7 Jul 2024 at 15:58

- 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.

### More Answers (1)

Umar on 7 Jul 2024 at 17:34

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

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

### 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.

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