How to define set of numbers without some special numbers?

classic Classic list List threaded Threaded
6 messages Options
Christian Hoehle Christian Hoehle
Reply | Threaded
Open this post in threaded view
|

How to define set of numbers without some special numbers?

Hi all,

i recently tried to do a plot of a function with a value range of (x |
(-100 <= x <= 100) \ {0; -3; 2}).
Any idea how to realize that?
Most probably i just didn't find the manual's page describing definition
of sets omitting special numbers... or yust didn't get the right keyword
to search for ;-)

Thanks for all hints.

Cheers,
Chris

harishankar ramachandran harishankar ramachandran
Reply | Threaded
Open this post in threaded view
|

Re: How to define set of numbers without some special numbers?

Let us say you have already defined a function f(x), and you want to plot it
over a range excluding some special points, {x_i}

    a < x_1 < x_2 <...< x_k < b

There is no built in function in scilab that plots this. However, it is a
simple matter to create plots that skip these points. User dependent issues:

1. Should the points on either side of a special point be connected?

2. Should the points on either side of a special point be equi-distant?

Given that info, it is a simple matter to create the set of disjoint line
segments that can be used to plot the function (using xpoly) Here is a
function that connects across special points, but ensures points on either
side are _roughly_ equidistant:

=================================
//* program to plot a function excluding a set of given points
function plote2d(a,b,exclude,n,f,optarg)
  //* the range is a to b
  //* exclude is a vector containing the points to exclude
  //* n is the total number of points to use
  //* f is the function to be invoked
  dx=(b-a)/(n-1); // the nominal spacing between points
  x0=[a; matrix(exclude,length(exclude),1); b];
  nn=ceil(diff(x0)/dx);
  j=0;
  xx=zeros(sum(nn),1);
  for i=1:length(nn)
    // printf("%d: (%f,%f)\n",nn(i),x0(i),x0(i+1));
    t=linspace(x0(i),x0(i+1),nn(i)+2)';
    xx(j+1:j+nn(i))=t(2:$-1);
    j=j+nn(i);
  end
  yy=f(xx);
  if isdef("optarg")
    s="plot2d(xx,yy,"+optarg+")";
    execstr(s);
  else
    plot2d(xx,yy)
  end
endfunction
function y=f(x)
  y=abs(log((x-1).^2)+log((x+0.5).^2));
endfunction

//* test code
clf
plote2d(-2,2,[-1 -0.5 1 1.5],501,f,'logflag=''nl''')
xtitle("a sample plot");
//* comments
// if singularities are too close to each other, we might end up
// with exactly one point between singularities.

// also, 'n' is a nominal number. The number of points actually
// used may slightly exceed n.

// The lines are connected across the singularities.

// Note that optional arguments are sent as a quoted string.
// Embedded quotes are handled by doubling them (see test code).
=================================

hari

On Thursday 21 May 2009 14:14, Christian Hoehle wrote:

> Hi all,
>
> i recently tried to do a plot of a function with a value range of (x |
> (-100 <= x <= 100) \ {0; -3; 2}).
> Any idea how to realize that?
> Most probably i just didn't find the manual's page describing definition
> of sets omitting special numbers... or yust didn't get the right keyword
> to search for ;-)
>
> Thanks for all hints.
>
> Cheers,
> Chris

--
Dr. Hari Ramachandran, Professor, 332B ESB, EE Dept, IIT-Madras
Interests: Nonlinear Optics, Nonlinear Waves, Plasma Physics, Particle
           Simulations, Computational Algorithms, Linux.
Off: 91-44-2257-4421                    Fax: 91-44-2257-0120
Res: 91-44-2663-1863             Home Email: [hidden email]

Samuel GOUGEON-3 Samuel GOUGEON-3
Reply | Threaded
Open this post in threaded view
|

Re: How to define set of numbers without some special numbers?

In reply to this post by Christian Hoehle
If you want to avoid evaluating the function at special points and to
connect
first neighbours accross special points, one has previously to remove these
ones from the list :
x=-100:100;
x2=setdiff(x,[0 -3 2]);
clf
plot2d(x2,1. ./x2 + 1. ./(x2 + 3) + 1. ./(x2 - 2));

If you want to manage divergent calculations (e.g. at some poles), try :
x=-100:100;
ieee(2);
y=1. ./x + 1. ./(x + 3) + 1. ./(x - 2); // example
clf
plot2d(x,y)  // no segment connected to |%inf| values are plotted

After this remark, you may set y value of  special x locations to %inf or
-%inf to use the same plotting feature (but buggy):
x=-100:100;
[v,kx]=intersect(x,[0 -2 3]);
y=sin(x/10);
y(kx)=%inf;
plot2d(x,y);   // see buggy segment (i am reporting it through bugzilla)

Regards
Samuel


----- Message d'origine -----
De : Christian Hoehle
Date : 21/05/2009 10:44:

> Hi all,
>
> i recently tried to do a plot of a function with a value range of (x |
> (-100 <= x <= 100) \ {0; -3; 2}).
> Any idea how to realize that?
> Most probably i just didn't find the manual's page describing definition
> of sets omitting special numbers... or yust didn't get the right keyword
> to search for ;-)
>
> Thanks for all hints.
>
> Cheers,
> Chris


Samuel GOUGEON-3 Samuel GOUGEON-3
Reply | Threaded
Open this post in threaded view
|

Re: How to define set of numbers without some special numbers?

----- Message d'origine -----
De : Samuel GOUGEON
Date : 22/05/2009 13:46:
>
> After this remark, you may set y value of  special x locations to %inf or
> -%inf to use the same plotting feature (but buggy):
> x=-100:100;
> [v,kx]=intersect(x,[0 -2 3]);
> y=sin(x/10);
> y(kx)=%inf;
> plot2d(x,y);   // see buggy segment (i am reporting it through bugzilla)
After further tests, this bug seems to appear when a single point is
isolated
(e.g. between two undrawable parts of the plot)(here at x=-1)
Therefore, the proposed answer to your initial question will work as long as
no point in the x-span is isolated (and that this plot2d() bug will not
be fixed).

Regards
Samuel


Samuel GOUGEON-3 Samuel GOUGEON-3
Reply | Threaded
Open this post in threaded view
|

Re: How to define set of numbers without some special numbers?

----- Message d'origine -----
De : Samuel GOUGEON
Date : 22/05/2009 14:06:
> After further tests, this bug seems to appear when a single point is
> isolated
> (e.g. between two undrawable parts of the plot)(here at x=-1)
This is not the reason. Other tests with un-isolated points also plot an
extra segment.
Bug report is http://bugzilla.scilab.org/show_bug.cgi?id=4537

SG


Christian Hoehle Christian Hoehle
Reply | Threaded
Open this post in threaded view
|

Re: How to define set of numbers without some special numbers?

Hari and Samuel,

thank you very much for your efforts. I found the keyword i was
searching for within one of Samuels posts: Setting ieee to 2 causes the
result of a division by null to be Inf or NaN. On plotting, segments
with an Inf or NaN element are ignored, so graphs with excluded values
within their definition range can be plotted (with last segment before
exclusion value missing, but that is ok when step width is small enough).

Thanks again,
Chris