Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Gaussian 3D plot

Subject: Gaussian 3D plot

From: JAMES

Date: 12 Apr, 2013 20:40:08

Message: 1 of 14

Hi,
I am trying to produce a 3D plot of a quasi-Gaussian function, shown below as the function Z=Z = (C0).*exp((((pI.*k0)./(D.*q.*(r.^2))).*(log(1+((r.*x)./k0))))-((pI.*x)./(D.*q.*r)))
with the variables as listed. Could anyone please help with the following error :
??? Error using ==> surf at 70
Z must be a matrix, not a scalar or vector.

The code is listed below, and for those interested, its a function to describe electrophoresis. I would like to include this in my thesis which is now due, so a prompt reply would be much appreciated.
 
C0=1:10;pI=1:10;k0=1:10;D=1:10;q=1:10;r=1:10;x=1:10;[X, Y] = meshgrid(-15:.1:15, -15:.1:15);
Z = (C0).*exp((((pI.*k0)./(D.*q.*(r.^2))).*(log(1+((r.*x)./k0))))-((pI.*x)./(D.*q.*r)))
surf(X,Y,Z);view(-36,36);axis equal;drawnow

Z =

    0.7358 1.5965 2.5076 3.4449 4.3978 5.3610 6.3312 7.3066 8.2858 9.2680

??? Error using ==> surf at 70
Z must be a matrix, not a scalar or vector.

Kind Regards
Jim

Subject: Gaussian 3D plot

From: Yehonatan

Date: 12 Apr, 2013 22:53:08

Message: 2 of 14

Hello Jim,
First of all i see you don't understand how to use surf. Before explaining to you how to use it i have to ask: What are you looking for? Do you want a 3d surface plot or do you want a 3d line plot. In using surface you are saying you have a surface like data which i don't see in you code. What i see is a line data. So if you are looking for a 3d line you can use plot3. If you are looking for a surface plot tell me and i will help you.

Subject: Gaussian 3D plot

From: Yehonatan

Date: 12 Apr, 2013 23:55:08

Message: 3 of 14

Hello again,
A closer look at your code brings another question. You used meshgrid to create X AND Y matrices but you did not use them when calculating Z. Furthermore, when using surf you used X,Y,Z. Why did you used X,Y with Z if Z wasn't calculated based on X,Y.
I don't understand what you are trying to do here. But i can start helping by explaining how to use surf. One way to use surf is to give it a grid, based on the first two elements(X,Y), and a third element z which will be the points on the grid. Like this:

x =
     1 2 3 4 5


y =

     1
     2
     3
     4
     5


z =

     3 1 4 5 3
     2 3 7 1 9
     2 5 1 5 3
     5 8 0 32 1
     3 1 4 6 2

Another way is to use meshgrid, which will be like this:

x =

     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5


y =

     1 1 1 1 1
     2 2 2 2 2
     3 3 3 3 3
     4 4 4 4 4
     5 5 5 5 5


z =

     1 2 3 4 5
     2 4 6 8 10
     3 6 9 12 15
     4 8 12 16 20
     5 10 15 20 25

You see how in both cases x and y act like a grid.
This are two ways which you can use surf with. If you are unable to use them for whatever reason i can offer you another solution.

Subject: Gaussian 3D plot

From: JAMES

Date: 13 Apr, 2013 00:32:10

Message: 4 of 14

Hi Yehonatan,
Many thanks for your response, I am obviously a novice at this, so please bear with me. I'm merely trying to produce a 3D plot of the function:
C = C0*exp((((pI*k0)/(D*q*(r^2)))*(ln(1+((r*x)/k0))))-((pI*x)/(D*q*r)))
I would prefer a view with contoured lines on a coloured 3D surface.
I am not having any luck with your suggestions yet but I am keen to know if you have alternative ideas.
I'm sure its only a few lines of code but I'm not a programmer, so any help is much appreciated.
Kind regards,
Jim

Subject: Gaussian 3D plot

From: Yehonatan

Date: 13 Apr, 2013 10:58:06

Message: 5 of 14

Hello Jim,
Obviously i don't understand your math and you don't how to use matlab. In order for me to help you we need to find a common ground. Think of this, if you are trying to produce a graph on your notebook what will you do. You will create data point with x,y and z data for each data point. Then you will draw these points on the graph and then you will connect a line between them. So, if you want to plot your function you need to create data points with data for each axis, just like in a notebook. If you find it hard, send me the code that produce this data points and i will create the graph, be sure to give me data points with data for each axis so if i want I'll be able to draw this on paper as well. You can give it to me in a vector or any way you want. Keep in mind that matlab will not magically completes the surface plot if you will not give sufficient data. Example:
If you give me this:
x = [1,2,3,4,5];
y = [1,2,3,4,5];
z = [1,2,3,4,5];

The surface will look like this:

Surface =
  
     1 0 0 0 0
     0 2 0 0 0
     0 0 3 0 0
     0 0 0 4 0
     0 0 0 0 5

 The surface is not complete because there is no data to complete it.
Hope this will help

Subject: Gaussian 3D plot

From: Yehonatan

Date: 13 Apr, 2013 16:06:07

Message: 6 of 14

Another thing,
To be clearer.
In the last post i showed you this:
x = [1,2,3,4,5];
y = [1,2,3,4,5];
z = [1,2,3,4,5];

Here, the data points are:
1) x = 1,y = 1,z = 1;
2) x = 2,y = 2,z = 2;
3) x = 3,y = 3,z = 3;
4) x = 4,y = 4,z = 4;
5) x = 5,y = 5,z = 5;

And the result surface was this:

Surface =
  
     1 0 0 0 0
     0 2 0 0 0
     0 0 3 0 0
     0 0 0 4 0
     0 0 0 0 5

What you see here is the z axis value. The location for each value is determined by
The correspond x and y value for that z value. If you give me your data points i can plot them or if you want to learn how i can explain to you in more detail. However, keep in mind that if you will not give sufficient data point the result surface may be not complete as shown by the zero value of the surface i showed you.

If for example i want to plot the function x^2 in 3d. I want that there will be 5 grid points on the x axis and 5 grid points on the y axis. That means i want to calculate the function for every possible x and y grid points. That means i need to calculate the function for 25 possible x and y grid points. In order for me to calculate the function for every possible grid point i use meshgrid like this:

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

Here i told meshgrid to create tow matrices that will contain all of the possibilities.
Here is how they look:

X =

     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5
     1 2 3 4 5


Y =

     1 1 1 1 1
     2 2 2 2 2
     3 3 3 3 3
     4 4 4 4 4
     5 5 5 5 5

Now, if i look at each correspond cell of the tow matrices like: cell (1,1) and cell(5,5) they give me a grid possibility. Now i need to calculate z for each correspond cell of the tow matrices like this:

Z = X.*Y;

Z =

     1 2 3 4 5
     2 4 6 8 10
     3 6 9 12 15
     4 8 12 16 20
     5 10 15 20 25

The calculation did this:
Z(1,1) = 1*1 and this Z(5,5) = 5*5 and more.

Now i have a z value for each x and y grid point value. These points i will give matlab to plot. However, not all functions can be generated like that. If you have a problem doing that just gives me the points you have and i will plot them. Keep in mind you give me points that have x,y and z axis values..

Subject: Gaussian 3D plot

From: JAMES

Date: 13 Apr, 2013 18:21:19

Message: 7 of 14

Hi Yehonatan,

Many thanks for your patience and thorough explanations. I understand the theory behind how the matrices define x & y data points and by computing an associated z (height) value a 3D plot can be generated. The main problem for me is that I do not have any data points to work from. Just a mathematical expression.
The scientific paper I got this formula from is available from Science Direct, or if you don't have access, I could email you a pdf. Assuming that you can access the database, the details are as follows:

Moving chemical reaction boundary and isoelectric focusing: I. Conditional equations for Svensson–Tiselius' differential equation of solute concentration distribution in idealized isoelectric focusing at steady state.
Journal of Chromatography A, 813 (1998) 153–17
Author: Cheng-Xi Cao

It's Equation No. 10, from Section 3.1

Kind Regards,

Jim
 

Subject: Gaussian 3D plot

From: dpb

Date: 13 Apr, 2013 19:40:43

Message: 8 of 14

On 4/13/2013 1:21 PM, JAMES wrote:
> Hi Yehonatan,
>
> Many thanks for your patience and thorough explanations. I understand
> the theory behind how the matrices define x & y data points and by
> computing an associated z (height) value a 3D plot can be generated. The
> main problem for me is that I do not have any data points to work from.
> Just a mathematical expression.
...

doc meshgrid

and follow the example of evaluation a function to plot a surface. Just
use your equation in place of the example one...

--

Subject: Gaussian 3D plot

From: Yehonatan

Date: 13 Apr, 2013 20:06:15

Message: 9 of 14

Ok,
James, you need to explain to me what you want to show here. You have a function,
You have variables, At least you need to know that you want to show a function based on one or two variables. If i want to show bird flight pattern i need to know what pattern i want to show. In one case i want to show the height of the bird flight based on the location on the earth so i show the height variable based on the latitude and longitude variable. Here is what we will do, you'll define what you want to show in the function and in the meantime send me the pdf so i will be able to see it. You need to know that i don't understand this math so if you can't provide me with data points to work with i don't think i can help you, unless you want to teach me what the pdf is about andi don't think you want to do that.

Subject: Gaussian 3D plot

From: Yehonatan

Date: 13 Apr, 2013 20:31:15

Message: 10 of 14

Furthermore,
It is not enough to have an expression. In order to show the expression i need to know what i want to show in the expression. For instance if i have f(x) = x^2, It is not enough to have this expression, if i want to show it visually i need to know what i want to show, for instance i want to show the function f(x) based on the variable x. This is minimum in order for me to help you. If all you have is an expression it's not enough. You need to know what you want to show in the expression.

Subject: Gaussian 3D plot

From: JAMES

Date: 13 Apr, 2013 21:55:08

Message: 11 of 14

Hi Yehonatan,
I've sent the files to your email address, as I'm restricted to plain text here. Her is a copy of the email:

Hi Yehonatan,
I've attached the paper as a pdf and and some images.

The function I've been trying to plot is shown in the paper as Equation 10, and I've also attached it, for convenience as quasi_gaussian_function.png.

It is a function describing the concentration of fluid particles in an electric field. The process is called isoelectric focusing, or more commonly known as electrophoresis. The quasi_gauss_ function is a mathematical statement about how the charged particles in a fluid, at random locations, move to a common plane called the isoelectric plane, under the influence of an electric field. However they do not simply form a flat plane but are focused more widely at the bottom and thinly at the top- like a Gaussian distribution in statistics.

I've plotted a different Gaussian function and attached the code so that you could see what I'm looking for.

I just need the same type of plot now for the quasi_gaussian function.

I hope I'm making some sense.

Many thanks,

Jim
 

Subject: Gaussian 3D plot

From: dpb

Date: 14 Apr, 2013 01:06:02

Message: 12 of 14

On 4/13/2013 4:55 PM, JAMES wrote:
...

> ... as I'm restricted to plain
> text here. ...
> The function I've been trying to plot is shown in the paper as Equation
> 10, and I've also attached it, for convenience as
> quasi_gaussian_function.png.

Post a link to one of the many free-hosting sites...or write the
equation out here--you'll have to translate it to Matlab code to
evaluate it to plot it anyway, may as well get started...

> It is a function describing the concentration of fluid particles in an
> electric field. ...

As posted earlier, look at the example you'll find in

doc meshgrid

of evaluating a function over a mesh in order to plot the surface...

--

Subject: Gaussian 3D plot

From: Yehonatan

Date: 14 Apr, 2013 09:45:08

Message: 13 of 14

Send you an email....

Subject: Gaussian 3D plot

From: dpb

Date: 14 Apr, 2013 14:57:34

Message: 14 of 14

On 4/12/2013 3:40 PM, JAMES wrote:
> Hi,
> I am trying to produce a 3D plot of a quasi-Gaussian function, shown
> below as the function Z=Z =
> (C0).*exp((((pI.*k0)./(D.*q.*(r.^2))).*(log(1+((r.*x)./k0))))-((pI.*x)./(D.*q.*r)))
>
> with the variables as listed. Could anyone please help with the
> following error :
> ??? Error using ==> surf at 70
> Z must be a matrix, not a scalar or vector.
>
> The code is listed below, and for those interested, its a function to
> describe electrophoresis. I would like to include this in my thesis
> which is now due, so a prompt reply would be much appreciated.
> C0=1:10;
   pI=1:10;
   k0=1:10;
   D=1:10;
   q=1:10;
   r=1:10;
   x=1:10;
   [X, Y] = meshgrid(-15:.1:15, -15:.1:15);
> Z =
> (C0).*exp((((pI.*k0)./(D.*q.*(r.^2))).*(log(1+((r.*x)./k0))))-((pI.*x)./(D.*q.*r)))
>
> surf(X,Y,Z);
   view(-36,36);
   axis equal;
   drawnow
>
> Z =
>
> 0.7358 1.5965 2.5076 3.4449 4.3978 5.3610 6.3312 7.3066 8.2858 9.2680
>
> ??? Error using ==> surf at 70
> Z must be a matrix, not a scalar or vector.

OK, I didn't look at the original post; only the follow-ons where the
equation wasn't given nor your attempt...

The error is precisely what it says--you've asked to plot a surface of
F(X,Y) but only evaluated Z on the vectors x,y not the results of the
X,Y grid you created w/ meshgrid.

You'll have to set the values of all the other constants to some
specific value and then evaluate the surface for those conditions to
draw an x,y surface plot.

Then, sotoo of

R=sqrt(X.^2+Y.^2);
Z =C0.*exp(((pI*k0./(D*q*R.^2)) ...
        .*(log(1+((R.*X)./k0))))-((pI.*X)./(D.*q.*R)));

will evaluate the function over the grid.

Looks to me like you'll have to choose the constants carefully if the
result is supposed to be real; otherwise the result is going to be complex.

But, that ought to get you going...

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us