1

What is the best way of plotting several functions with different domains into the same plot? Is there a way to do this with plot2d, or do I have to use draw2d instead?

I especially like the possibility in plot2d to give several functions in a list, whereas I would have to add the different functions in draw2d as separate parameters, if I understand the documentation correctly.

An example of what I mean:

f(x, a) := sqrt(a) * exp(-(x-a)^2);
fmax(x) := sqrt(x);

In this example I would like to plot f(x, a) for several a (e.g. using makelist(f(x, a), a, [0, 0.5, 1, 2, 5])) from -1 to 10 and fmax from 0 to 5 (to show where the maxima of the f(x, a) family of curves are located).

3 Answers 3

3

You can try draw2d

f(x, a) := sqrt(a) * exp(-(x-a)^2);
fmax(x) := sqrt(x);

flist: makelist(f(x, a), a, [0, 0.5, 1, 2, 5]);

par: map(lambda([f], explicit(f, x, -1, 10)), flist);
par: append([explicit(fmax, x, 0, 5), color=red], par);

load(draw);
apply(draw2d, par);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this seems to work. Thanks especially for showing the combination of lists and apply to expand the content as arguments.
1

This was frustrating me for hours but I found a way to have multiple differently domained functions on the same graph.

wxplot2d([if x < 0 then -x else sin(x), if x > -1 then x^2],[x,-%pi,%pi],[y,-2,2]);

Comments

0

One approach I am not particularly happy with is to declare the functions with smaller domains as parametric curves, with the x axis parameter being simply x:

f(x, a) := sqrt(a) * exp(-(x-a)^2);
fmax(x) := sqrt(x);
plot2d(endcons([parametric, x, fmax(x), [x, 0, 5], [nticks, 80]],
               makelist(f(x, a), a, [0, 1/2, 1, 2, 5])),
       [x, -1, 10]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.