How to Superimpose Multiple Plots in Mathematica?

In summary: You can't, unless you say what c and a are. Because you can't plot an expression, you can only plot a function which takes one number (x) and returns another number (y). In principle you could define a function likef[x_] := Module[{a, c}, x^2]which doesn't depend on a or c, but that's not a very useful function. The whole point of a function is that you can plot it for different values of its parameters, so it doesn't make much sense to plot an expression containing parameters which are not specified.Another example: can you plot f(x) = a + b x? You can't, because it depends on two parameters a and b. You can
  • #1
abbeynewton
31
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
  • #2
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:
  • #3
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)
 
  • #4
thanks CompuChip...this was really helpful
 
  • #5
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?
 
  • #6
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.
 
  • #7
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.
 
  • #8
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??
 
  • #9
How should it?

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

1. What is Mathematica Input Question?

Mathematica Input Question is a feature of the Mathematica software that allows users to input mathematical equations and expressions, and receive the corresponding output or solution.

2. How do I use Mathematica Input Question?

To use Mathematica Input Question, open the Mathematica software and type in your mathematical equation or expression using the appropriate syntax. Then, press the Enter key to receive the output or solution.

3. Can Mathematica Input Question handle complex equations?

Yes, Mathematica Input Question is designed to handle complex mathematical equations and expressions, including algebraic, trigonometric, and calculus equations. It can also handle a wide range of mathematical functions and operations.

4. Are there any tips for using Mathematica Input Question effectively?

One tip for using Mathematica Input Question effectively is to make use of the built-in documentation and tutorials provided by the software. These resources can help you learn the syntax and functions available in Mathematica and improve your proficiency in using the software.

5. Can I save my Mathematica Input Question inputs and outputs?

Yes, you can save your Mathematica Input Question inputs and outputs by using the "Save" feature in the software. This allows you to refer back to your work or share it with others in the future.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
234
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top