How to Superimpose Multiple Plots in Mathematica?

  • Thread starter Thread starter abbeynewton
  • Start date Start date
  • Tags Tags
    Input Mathematica
AI Thread Summary
To superimpose multiple plots in Mathematica, the correct approach is to pass the functions as a list within the Plot command. The user initially attempted to define functions using conditional replacements, which led to errors. Instead, defining a function of two variables or using a simpler function definition is recommended. Additionally, when specifying limits for plotting, Mathematica requires numerical values rather than symbolic ones, necessitating the use of specific numerical boundaries for the plots. Overall, understanding how to properly define functions and set limits is crucial for successful plotting in Mathematica.
abbeynewton
Messages
31
Reaction score
0
Hi everyone, i have been recently trying to get familiar with the Mathematica software for a project am working on. In the course of that i encountered a problem on graphics. here's the question:
Superimpose the plots y=√(x^2+a), √a≤x≤√a+3 for the values a=1,a=4,a=9 in the same figure.

Now the challenge is to input this correctly into Mathematica. I tried doing it this way:

eqy1[x_]:=√(x^2+a)/; a -> 1
eqy2[x_]:=√(x^2+a)/; a -> 4
eqy3[x_]:=√(x^2+a)/; a -> 9
Plot[eqy1[x],eqy2[x],eqy3[x], {x, √a, √a+3}, PlotRange -> {-3,3}, PlotStyle->{red,green,blue}]

when i entered this ,it sent an error message saying that an option is needed beyond position 2 and all of that.

Please i need any clue, hint or help on how to input it correctly. I have only a month to learn everything i need to know in Mathematica for my final year project, and i need to get this problem before i go through. thanks
 
Physics news on Phys.org
The function you want to plot is just the first argument. If you want to plot multiple functions in the same graph, you need to pass them as a list.
Try
Code:
Plot[{eqy1[x],eqy2[x],eqy3[x]}, {x, √a, √a+3}, PlotRange -> {-3,3}, PlotStyle->{red,green,blue}]

If you are using Mathematica 7 or higher, this will do just fine.
 
Last edited:
By the way, I think you meant to use /. instead of /; in your definitions.
Code:
expr /. a -> b
replaces a with b everywhere in the expression expr.

/; is more like a conditional operator, for example
Code:
abs[x_] := x /; x >= 0
abs[x_] := -x /; x < 0

You don't even really need it here, you could just define a function of two variables
Code:
f[x_, a_] := √(x^2+a)
or
Code:
Subscript[f, a_][x_] := √(x^2+a)
(which you can also write as fa_[x_] using Control + -)
and then plot f[x, 1], f[x, 4] and f[x, 9] (or f1[x], f4[x] and f9[x] respectively)
 
thanks CompuChip...this was really helpful
 
But i still have a problem with the limit...mathematica is telling me that sqrt[a] in the limit is not a machine size real number...what should i do?
 
If you can kill the kernel, restart it, evaluate the whole notebook, select all the cells with input, output and error message and paste that here then it should be possible to quickly identify exactly what the problem is.
 
The probelm is exactly what it says.
If you plot a graph then you can only plot it if you give Mathematica numerical boundaries.
For example it can plot f(x) between x = -3 and x = 3, or between x = 1200 and x = 1987, but it cannot plot between x = -a and x = a if it doesn't know what a is.

If this is what you want, you will probably end up doing something like
Code:
Show[{
  Plot[f[x, 1], {x, Sqrt[1], Sqrt[1+3]}, PlotRange -> {-3,3}, PlotStyle->red],
  Plot[f[x, 4], {x, Sqrt[4], Sqrt[4+3]}, PlotRange -> {-3,3}, PlotStyle->green],
  Plot[f[x, 9], {x, Sqrt[9], Sqrt[9+3]}, PlotRange -> {-3,3}, PlotStyle->blue]
}]

or you will have to plot them all on the smallest interval including them all, i.e.
Code:
Plot[{f[x, 1], f[x, 4], f[x, 9]}, {x, Sqrt[9], Sqrt[9+3]}, PlotRange -> {-3,3}, PlotStyle->{red, green, blue}]
This will plot all three of them between x = 3 and x = 2 sqrt(3), the PlotRange will take care of cutting off the functions on the vertical axis.
 
thanks for the explanation...was really helpful...but does is mean that Mathematica does not recognize, squareroots of unknown variables, and you have to define them,even when using intervals??
 
How should it?

Can you plot f(x) = x² between x = -a and x = c?
 
Back
Top