How to Create a 3D Distribution Plot in Matlab?

Click For Summary

Discussion Overview

The discussion revolves around creating 3D distribution plots in Matlab, specifically focusing on visualizing parameters like density or temperature in a cone-shaped distribution. Participants share their attempts, challenges, and code snippets related to 3D plotting, optimization of Matlab code, and issues with neural networks and image processing.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their goal of visualizing a 3D distribution that resembles a cone shape, where density decreases with distance from the tip, and seeks guidance on applying a normal distribution in 3D.
  • Another participant mentions performance issues with their Matlab code, specifically regarding the computation time for a large matrix and seeks optimization advice.
  • A third participant discusses segmenting histological features from RGB images using color vectors and dot products, asking for help in determining pixel colors based on vector magnitudes.
  • One user shares their experience with a multi-layer perceptron (MLP) neural network in Matlab, noting difficulties in training the network with certain functions and asking for potential corrections in their back-propagation implementation.
  • Another participant expresses the need for a 3D plot using random numbers and discusses challenges with memory limits and achieving a visually appealing surf plot, mentioning attempts with various functions like sortrows, reshape, and gridfit.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the best methods for achieving their respective goals, as multiple competing views and unresolved issues remain throughout the discussion.

Contextual Notes

Some participants express uncertainty regarding the optimization of their Matlab code, the effectiveness of their neural network training, and the proper use of functions for 3D plotting. There are mentions of limitations related to memory usage and the complexity of the tasks being attempted.

Who May Find This Useful

Individuals interested in Matlab programming, particularly in the context of 3D plotting, optimization techniques, neural networks, and image processing may find the discussion relevant.

galanos
Messages
1
Reaction score
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
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
 
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:
 
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:
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.
 
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
 
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?
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K