Subject: using plot2d with numerical integration function
From: Jaime E. Villate
Date: Mon, 28 May 2007 15:28:51 +0100
On Mon, 2007-05-28 at 14:52 +0200, Detlef Schmicker wrote:
> I try to plot2d (or wxplot2d) a function containing quad_qad
> integration. This numerical function only works, if the variable on
> the x achses of my plot is defined. Now plot2d seems to call the
> function without defined variable once.
> How can I avoid this.
> () ff:(sin(x)^2-1/2)*abs(x-y)*exp(-(x-y)^2/5);
> () plot2d(quad_qag(ff,x,-100,100,6),[y,-5,5]);
Hi,
even if you managed to make plot2d evaluate ff for the corresponding
y value on the horizontal axis before calling quad_qag, you still have
two problems that will make plot2d fail:
1) quad_qag returns a list with four values, and not just the value
that you want to plot on the vertical axis.
2) plot2d uses an adapting step-size method that works fine for
analytical functions but might lead to a very time-consuming calculation
in the case of numerical functions.
For a numerical function like the one you have, I recommend that you use
the "discrete" option of plot2d. Choose your horizontal-axis values and
make a list with them. For instance, 101 points on the interval
[y,-5,5]:
yy: makelist(i/10.0-5.0, i, 0, 100)$
then create another list with the corresponding numerical values of the
integral:
int: makelist(first(quad_qag(ev(ff,y=z),x,-100,100,6)), z, yy)$
finally, plot the points:
plot2d([discrete, yy, int])$
It is better to start with a few points, for instance 5, to make sure
the numerical calculations do not take too much time, and you can then
increase that number.
Regards,
Jaime Villate