3D Distribution Plot on Matlab

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..
 
Physics news on Phys.org
  • #2
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;
    end
end
 
  • #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 :redface:
 
  • #4
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));
	end

	W2n=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));


		end
	end


	W1n=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
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
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
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?
 

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

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
737
Back
Top