How to Create a 3D Distribution Plot in Matlab? (2024)

  • Forums
  • Mathematics
  • MATLAB, Maple, Mathematica, LaTeX
  • MATLAB
  • Thread startergalanos
  • Start date
  • Tags
    3dDistributionMatlabPlot

In summary, The conversation is about building a Matlab code for the distribution of a parameter in 3D and visualizing it as a cone shape. The individual has tried using a Normal (Gaussian) distribution for the 2D case and is now struggling to apply it for the 3D case. They are using equally sized cells to define a 3D environment and assigning values for the density according to a distribution function. They have shared their code and are looking for help in optimizing it. They are also working on a MLP neural network with back-propagation in Matlab and are having trouble handling curves in a function and scaling values. They have shared their code and are looking for suggestions on how to correct it.

  • #1

galanos

2
0

Im trying to build a Matlab code for the distribution of a parameter (such as density or temperature) in 3D, then visualise the distribution also in 3D. At the end, the distribution plot should look like a cone shape where the highest value for the density (or temperature) is at the tip of the cone and lower values as moving down and outwards on the cone. So that density should decrease both by horizontal and vertical distance from the cone tip. I've tried Normal (Gausian) distribution for the 2D case and it worked but i don't know how to apply that for 3D case and I am really lost!

What I am intended to do is to use many equally sized cells (cubes) to define a 3D environment (volume) and then assign values for the density to each of these cells according to a distribution function (normal dist func.)

Here is what I've tried so far for the 2D case. You may see several funny mistakes since I am almost a beginner on Matlab! Please give me some idea and light up my world!

% Standard Normal Distribution of Density
clear all,close all
% Parameters:
% x:cell number
% l: total length of cells in a layer
% h: distance from the cone tip
% d_cell: dimension of the cell
% i: layer number
% m: mass in each layer (constant)

theta = pi/10; % cone angle (18 deg)
d_cell = 0.1; % cell dimension
cell_volume = d_cell^3;
mu = 0; % mean value of the normal distribution
m = 0.005; %row mass

symvar h; % symbolic variable
h = 1:60;
l = 2*h*tan(theta/2);
% plot(h,l(h));

% number of cells in each layer:-----------------------------------------
n = l/d_cell;
n = fix((n(h)+1)/2)*2-1; % rounds to the nearest smaller odd number

% standard deviation of the density distribution:------------------------
sigma = [];
sigma(1) = 1;
for j = 2:60
sigma(j) = sigma(j-1)*(n(j)/n(j-1));
end

% normal distribution of density on each layer:--------------------------
x = cell(60,1);
y = cell(60,1);
N = cell(60,1);
coef = cell(60,1);
for i = 1:60
t = (n(i)-1)/2;
x{i,1} = 1:t;
y{i,1} = (exp(-(x{i,1}-mu).^2/(2*(sigma(i)).^2)))./(sigma(i)*sqrt(2*pi));
N{i,1} = y{i,1}/max(y{i,1});
coef{i,1} = m/sum(cell_volume*N{i,1});
N{i,1} = coef{i,1}*N{i,1};
end

plot(x{i,1},N{i,1});

Thanks in advance for any help..

  • #2

dabargo

2
0

Ok, so i wrote a small MATLAB program but it takes forever for MATLAB to calculate it. Since I'm not very knowledgeable in optimizing code i hoped somebody could help me here. The only thing i did was use preallocation of my variable 'conditionnr'. The H matrix I'm using is a pretty big one (433x270 double).

Code:

k=1;conditionnr = zeros(1,size(H,1));while(k<size(H,1)+1) for i=1:size(H,1) Htemp = H; H(i,:)=[]; conditionnr(i) = max(svd(H))/min(svd(H)); H=Htemp; end [m,j] = min(conditionnr); H(j,:)=[]; if size(H,1)==size(H,2) k=size(H,1)+1; endend
  • #3

Hey all,
Hope everyone is okay. I have RGB colour images of different types of pathologies. I would like to segment interesting histological feautres from the RGB images based on what colour they are. I have stained them using H&E and want to just have the eosin stain left in my image, a blue/purple colour. I can kind see how to do this and I have defined a background vector from the origin of the RGB colour space, a desired stain vector and an undesired stain vector.

I have been mapping where the different stains will projection on to the desired vector and the normal vector to the undesired and the desired vectors. I am working in Matlab, because I am only human lol, and have successfully found where the stains should be in terms of the desired and normal vectors by using the dot product with respect to each vector.

Can anyone imagine away to find out what colour the pixels of the image should be based on their magnitudes along the normal and desired component vectors?

Thanks for your time
Physical 101 How to Create a 3D Distribution Plot in Matlab? (1)

  • #4

GProgramer

10
0

Hello again all! In a followup to my last question, I'm making a MLP neural network with back-propagation in matlab. The problem is, it seems not to be able to handle the curves in a function well, and also doesn't scale well with the values. It can for example reach 80% of the cos(x) but if I put 100*cos(x) it will just not train at all.

What is even weirder is, that some functions it can train well to, while others it just doesn't work at all..
For example:
Well trained: http://img515.imageshack.us/img515/2148/coscox3.jpg

Not so well: http://img252.imageshack.us/img252/5370/cos2d.jpg (smoothness from being left a long time)

Wrong results, stuck like this: http://img717.imageshack.us/img717/2145/ex2ug.jpg

This is the algo I'm trying to implement:

http://img594.imageshack.us/img594/9590/13012012001.jpg

http://img27.imageshack.us/img27/954/13012012002.jpg

And this is my implementation:

Code:

close all;clc;j=[4,3,1];i=[1,j(1),j(2)];X=0:0.1:pi;d=cos(X);%-----------Weights------------%%-----First layer weights------%W1p=rand([i(1)+1,j(1)]);W1p=W1p/sum(W1p(:));W1=rand([i(1)+1,j(1)]);W1=W1/sum(W1(:));%-----Second layer weights------%W2p=rand([i(2)+1,j(2)]);W2p=W2p/sum(W2p(:));W2=rand([i(2)+1,j(2)]);W2=W2/sum(W2(:));%-----Third layer weights------%W3p=rand([i(3)+1,j(3)]);W3p=W3p/sum(W3p(:));W3=rand([i(3)+1,j(3)]);W3=W3/sum(W3(:));%-----------/Weights-----------%V1=zeros(1,j(1));V2=zeros(1,j(2));V3=zeros(1,j(3));Y1a=zeros(1,j(1));Y1=[0 Y1a];Y2a=zeros(1,j(2));Y2=[0 Y2a];O=zeros(1,j(3));e=zeros(1,j(3));%----Learning and forgetting factor-----%alpha=0.1;etha=0.1;sortie=zeros(1,length(X));while(1)n=randi(length(X),1);%---------------Feed forward---------------%%-----First layer-----%X0=[-1 X(:,n)];V1=X0*W1;Y1a=tanh(V1/2);%----Second layer-----%Y1=[-1 Y1a];V2=Y1*W2;Y2a=tanh(V2/2);%----Output layer-----%Y2=[-1 Y2a];V3=Y2*W3;O=tanh(V3/2);e=d(n)-O;sortie(n)=O;%------------/Feed Forward-----------------%%------------Backward propagation---------%%----Output layer-----%delta3=e*0.5*(1+O)*(1-O);W3n=W3+ alpha*(W3-W3p) + etha * delta3 * W3;%----Second Layer-----%delta2=zeros(1,length(Y2a));for b=1:length(Y2a)delta2(b)=0.5*(1-Y2a(b))*(1+Y2a(b)) * sum(delta3*W3(b+1,1));endW2n=W2 + alpha*(W2-W2p)+ (etha * delta2'*Y1)';%----First Layer-----%delta1=zeros(1,length(Y1a));for b=1:length(Y1a)for m=1:length(Y2a) delta1(b)=0.5*(1-Y1a(b))*(1+Y1a(b)) * sum(delta2(m)*W2(b+1,m));endendW1n=W1+ alpha*(W1-W1p)+ (etha * delta1'*X0)'; W3p=W3;W3=W3n;W2p=W2;W2=W2n;W1p=W1;W1=W1n;figure(1);plot(1:length(d),d,1:length(d),sortie);drawnow;end

My question is, what can I do to correct it?
My guesses so far are, I either have something wrong in the back propagation, specifically in calculating delta and the weights. Or I have the weights initialized wrong (too small, or not dependent on the initial input)..

Last edited by a moderator:

  • #5

antonbe

1
0

I want to get a 3-d plot in Matlab, looking like the surf function. The drawback is that they are 3 vectors, which basically have random numbers. The vector is also around 7500 numbers long, so to make a matrix with all numbers will exceed the memory significantly. The x-values are within -0.12 to -0.05, the y-values are within 0.085 to 0.13 and the z-values are within 28 to 48.

I have tried sortrows and reshape to try to make matrices, but the surf plot looks odd when I do. I have also tried to use gridfit, but I don't know if I'm using it wrong, because it becomes a bent line. Using plot3, I get to see how I want it to look, like a mountain with two peaks. I would be satisfied with that unless it was for the colors in surf, which I can not add to plot3. I still think gridfit is the solution I'll try to use, but I hope someone can tell me what I need to change with the code, or if I should use some other code.

The current code I'm trying is:

xnodes=-0.13:0.0005:-0.085;
ynodes=0:0.0005:0.5;
figure(54);
clf;
[XCA,YCA,IMA]=gridfit(XC,YC,IM,xnodes,ynodes);
surf(XCA,YCA,IMA)

where XC,YC and IM are my x,y and z-vectors I have.

  • #6

Rickysan

1
0

Hey guys I am in desperate need of help. I am struggling in finding code for the shooting method for second order BVP. I use Eulers method for IVP but the problem is I am still unsure of how it would look in code. Would it maybe be better to use ODE45-Runga-kutta instead? I have had a try but to no success. The problem is (I think) is that my inputs don't talk to each other and that some steps are missing. Can someone please give me the code to solve:

y′′ = y + 2/3*e^t, y(0) = 0, y(1) = 1/3*e

Or point out where I made my mistake in my code. I am honestly lost atm:

%attempt at shooting sol using Euler for
% (d/dx)(dT/dx)=2, y(0)=1, y(1)=0
% If Value=T and Slope=dT/dx systesm is
% dValue/dx=Slope, Value(0)=1;
% dSlope/dx=0;, Slope(0)=Unknown--we need to guess
function solve()
x=0; %initial point
y0=1; %initial value at x=0
y'=-1; %initial Guess for slope
Vknown=0; %Known Value at x =1
del=0.1; %solution step size
Diff=10; %difference between predicetd abnd knonw value ay x =1
inter=[x,Vknown];

while abs(Diff)>0.001
euler(inter,yo,n);...
Slope=Slopei;
Value=Valuei;
for i=1:10
Value=Value+del*Slope;
Slope=Slope+del*2; %very simple in this case
end

Diff=Value-Vknown; %Difference between predicetd and known Value

% Update Slope(0) guess
Slopei=Slopei-1*Diff

end
end

function [t,y]=euler(inter,yo,n)
t(1)=inter(1);y(1)=y0;
h=(inter(2)-inter(1))/n;
for i=1:n
t(i+1)=t(i)+h;
y(i+1)=eulerstep(t(i),y(i),h);
end
plot(t,y)
end

function eulerstep(t,y,h)
y=y+h*ydot(t,y);
end

function z=ydot(t,y)
z=t*y+t^3
end

  • #7

NewtonsHead

26
0

Hey guys,

I am taking an intro to MATLAB course and it gave me the idea to make a program to time my rubiks cube solves. I'm not too experienced with the timer functions (tic toc, timer) so I don't know where to begin.

What I am looking for in my program:
-A cue to press a key that starts the timer
-timer stops when another button is pressed
-final time is displayed
-it would be even better if the running time was displayed during it

Does anybody know how I could make such a program or give me some ideas to put me on the right track?

Related to How to Create a 3D Distribution Plot in Matlab?

What is a 3D distribution plot on Matlab?

A 3D distribution plot on Matlab is a type of visualization used to represent the distribution of data in three dimensions. It is a combination of a scatter plot and a histogram, where the data points are represented by markers and the density of points is shown using color or height.

How do I create a 3D distribution plot on Matlab?

To create a 3D distribution plot on Matlab, you can use the "scatter3" function to plot the data points and the "hist3" function to create a histogram of the points. You can then use the "view" function to adjust the viewing angle and "colormap" function to change the color scheme.

What are the benefits of using a 3D distribution plot on Matlab?

A 3D distribution plot on Matlab allows you to visualize the distribution of data in three dimensions, which can be useful for identifying patterns and relationships that may not be apparent in 2D plots. It also provides a more comprehensive view of the data compared to traditional scatter plots or histograms.

Can I customize the appearance of a 3D distribution plot on Matlab?

Yes, you can customize the appearance of a 3D distribution plot on Matlab by adjusting the viewing angle, color scheme, marker style, and other plot properties such as axis labels and title. You can also add additional elements such as a legend or grid lines.

Are there any limitations to using a 3D distribution plot on Matlab?

One limitation of using a 3D distribution plot on Matlab is that it can become cluttered and difficult to interpret if there are too many data points. It is also important to carefully choose the viewing angle and color scheme to accurately represent the data. Additionally, 3D plots may not be suitable for all types of data or research questions.

Similar threads

MATLABPlots and FFT of Rows or Columns of Data

  • MATLAB, Maple, Mathematica, LaTeX
    Replies
    8
    Views
    421

    MATLABHow to Create a 3D Distribution Plot in Matlab?

    • MATLAB, Maple, Mathematica, LaTeX
      Replies
      2
      Views
      9K

      MATLABHow to plot an orbit around the Earth in MATLAB

      • MATLAB, Maple, Mathematica, LaTeX
        Replies
        2
        Views
        2K

        MATLABMATLAB IFFT doesn't match the analytical one

        • MATLAB, Maple, Mathematica, LaTeX
          Replies
          10
          Views
          2K

          MATLABPlotting Reaction Rate vs Temperature with MATLAB

          • MATLAB, Maple, Mathematica, LaTeX
            Replies
            4
            Views
            1K

            MATLABHow to plot flow data from an orifice in MATLAB without overlapping the plots?

            • MATLAB, Maple, Mathematica, LaTeX
              Replies
              2
              Views
              2K

              MATLABPlotting a 3D image from a matrix for a complex domain

              • MATLAB, Maple, Mathematica, LaTeX
                Replies
                2
                Views
                1K

                MATLABHow do I write this chua oscillator to an audio file in matlab?

                • MATLAB, Maple, Mathematica, LaTeX
                  Replies
                  5
                  Views
                  1K

                  MATLABHow to change the frequency values inside a time domain signal phase

                  • MATLAB, Maple, Mathematica, LaTeX
                    Replies
                    1
                    Views
                    869

                    MATLABHow to Plot a 3D Minimization Function in MATLAB?

                    • MATLAB, Maple, Mathematica, LaTeX
                      Replies
                      1
                      Views
                      1K
                      • Forums
                      • Mathematics
                      • MATLAB, Maple, Mathematica, LaTeX
                      How to Create a 3D Distribution Plot in Matlab? (2024)

                      FAQs

                      How do you make a 3D plot of data in MATLAB? ›

                      plot3( X , Y , Z ) plots coordinates in 3-D space.
                      1. To plot a set of coordinates connected by line segments, specify X , Y , and Z as vectors of the same length.
                      2. To plot multiple sets of coordinates on the same set of axes, specify at least one of X , Y , or Z as a matrix and the others as vectors.

                      How to plot 3D curves in MATLAB? ›

                      To convert a 2-D graph to 3D in MATLAB, you must use the 'plot3' function. This function will allow you to plot data in three dimensions. To use this function, you will need to provide three vectors: one for the x-axis, one for the y-axis, and one for the z-axis.

                      How to create a 3D model in MATLAB? ›

                      Direct link to this answer

                      In MATLAB, the patch function can be used to generate a 3D model by specifying the vertices and faces of the object. This function provides a convenient way to plot and visualize 3D objects in MATLAB. patch('Vertices', vertices, 'Faces', faces, 'FaceColor','red');

                      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

                      Can we have multiple 3D plots in MATLAB? ›

                      Explanation: The plot3() function is a pre-defined function in MATLAB. So, it will allow the use to generate multiple 3d plots. This is inherent to the system. 8.

                      What is a 3-D plot of a function? ›

                      f — 3-D function to plot

                      Specify a function of the form z = f(x,y) . The function must accept two matrix input arguments and return a matrix output argument of the same size.

                      What is the angle of a 3-D plot in MATLAB? ›

                      MATLAB automatically selects a viewpoint that is determined by whether the plot is 2-D or 3-D: For 2-D plots, the default is azimuth = 0° and elevation = 90°. For 3-D plots, the default is azimuth = -37.5° and elevation = 30°.

                      How to plot a graph in MATLAB using data? ›

                      Create Simple Line Plots

                      Create a table containing three variables. Then pass the table as the first argument to the plot function followed by the names of the variables you want to plot. In this case, plot the Input variable on the x-axis and the Output1 variable on the y-axis.

                      Can MATLAB be used for 3D modeling? ›

                      You can use MATLAB® to create a 3D environment, build actors, and view the 3D environment in the Simulation 3D Viewer. To learn how to create and view 3D simulations in the Unreal Engine simulation environment, follow these examples.

                      Can you create a 3D matrix in MATLAB? ›

                      You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array. Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension.

                      How to plot visualization in MATLAB? ›

                      Visualize Data with MATLAB
                      1. Click Apps > MATLAB Visualizations.
                      2. Click New to start your visualization.
                      3. Select a template or an example with sample code, which you can run and explore the results.
                      4. Click Create.

                      Can you make a 3D plot in Excel? ›

                      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.

                      How to plot data from spreadsheet in MATLAB? ›

                      Save your Excel file as an Excel Workbook in the MATLAB folder to ensure it can be imported correctly. Use the command p = plot(indep, dep1, indep, dep2) in the MATLAB command window to create a graph. Enter the command grid into the MATLAB command window to add grid lines to your graph.

                      How to make a graph in MATLAB using data? ›

                      Plotting Data Using MATLAB
                      1. Step 1: Opening the Program. ...
                      2. Step 2: Creating a Script File. ...
                      3. Step 3: Beginning a Script File. ...
                      4. Step 4: Creating or Importing Data. ...
                      5. Step 5: Creating the Plot. ...
                      6. Step 6: Adding Data Markers. ...
                      7. Step 7: Adjusting the Marker Size. ...
                      8. Step 8: Changing the Plot Color.

                      How to plot 3D radiation pattern in MATLAB? ›

                      Use the patternCustom function to plot the field data in 3-D. This function also allows you to view the sliced data. Alternatively, use the polarpattern object to visualize the field data in 2-D polar format.

                      How do you make a two dimensional plot in MATLAB? ›

                      Create Common 2-D Plots
                      1. The plot function creates simple line plots of x and y values.
                      2. Line plots can display multiple sets of x and y data.
                      3. The stairs function creates a stairstep plot. ...
                      4. The errorbar function draws a line plot of x and y values and superimposes a vertical error bar on each observation.

                      How to create a surface plot in MATLAB? ›

                      surface( X , Y , Z ) creates a primitive, three-dimensional surface plot. The function plots the values in matrix Z as heights above a grid in the x-y plane defined by X and Y . The color of the surface varies according to the heights specified by Z .

                      Top Articles
                      Latest Posts
                      Article information

                      Author: Kieth Sipes

                      Last Updated:

                      Views: 6144

                      Rating: 4.7 / 5 (67 voted)

                      Reviews: 82% of readers found this page helpful

                      Author information

                      Name: Kieth Sipes

                      Birthday: 2001-04-14

                      Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

                      Phone: +9663362133320

                      Job: District Sales Analyst

                      Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

                      Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.