help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: creating a plot of a self-defined function


From: Erik Leunissen
Subject: Re: creating a plot of a self-defined function
Date: Mon, 17 Feb 2014 13:09:56 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0

On 02/17/2014 11:35 AM, Juan Pablo Carbajal wrote:
On Mon, Feb 17, 2014 at 10:47 AM, Erik Leunissen <address@hidden> wrote:
function C = myfunc (x, t, M=100.0 , A=1.0, v=1.0, Dx=2.0, k=1.0)
        C=M/A*exp(-(x-v*t)^2/(4*Dx*t)-k*t)/(2*sqrt(pi*Dx*t));
endfunction

Ok, the problem is the following: You are asking octave to divide by
the matrix t. Since t can't be inverted, octave warns you that the
condition number of the inverse ("rcond") is below machine precision
(the inverse has a 0 singular value, i.e. not inverted). Ok, but where
did you ask for a matrix division?
Well, ezmesh internally makes a mesh of the arguments of the functions
and then it passes them to your function. If you change your function
defintion to

function C = myfunc (x, t, M=100.0 , A=1.0, v=1.0, Dx=2.0, k=1.0)
  S = Dx*t;
  C = M/A * exp ( -(x-v*t).^2 ./ (4*S) - k*t ) ./ (2*sqrt(pi*S) );
endfunction
You will get no warning and the plot will be produced.
Note the "./" and the ".^" opertors. This are element-wise operators.
It allows the caller to evaluate on matrix input arguments as if they
were a collection of different input pairs.


Thanks very much for your explanation, Juan Pablo (the code does what I wanted).

Your answer also touches on something else that I wanted to learn as a next step: how to make the function operate on matrices/vectors.
Thanks for being ahead of that; I'm going to explore that subject further.


Erik Leunissen
--



Note that you can achieve the same plot by using meshgrid, i.e.
[X Y] = meshgrid (linspace (-5,10,20), linspace (0,10,20) );
Z = myfunc (X, Y);
mesh (Z)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]