How to Superimpose Multiple Plots in Mathematica?

  • Thread starter Thread starter abbeynewton
  • Start date Start date
  • Tags Tags
    Input Mathematica
Click For Summary

Discussion Overview

The discussion revolves around how to superimpose multiple plots in Mathematica, specifically for the function y=√(x^2+a) over the interval √a≤x≤√a+3 for different values of a. Participants are seeking assistance with inputting the correct syntax and handling variable limits in Mathematica.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their attempt to plot multiple functions and encounters an error related to the syntax used for defining the functions and the limits.
  • Another participant suggests using a list to pass multiple functions to the Plot command, indicating that this is the correct approach in Mathematica 7 or higher.
  • A further contribution points out a potential error in the use of conditional operators in function definitions, recommending a different approach to defining the function with two variables.
  • One participant expresses a continuing issue with limits, specifically that Mathematica does not recognize square roots of unknown variables as valid numerical boundaries.
  • Another participant explains that Mathematica requires numerical boundaries for plotting and provides examples of how to structure the Plot command to avoid errors.
  • A later reply questions whether Mathematica can handle square roots of unknown variables in intervals, prompting further discussion on the nature of variable limits in plotting.

Areas of Agreement / Disagreement

Participants generally agree on the need for numerical boundaries in plotting but express differing views on the handling of variable limits and the correct syntax for function definitions. The discussion remains unresolved regarding the specific limitations of Mathematica in recognizing square roots of unknown variables.

Contextual Notes

Limitations include the dependence on specific versions of Mathematica and the unresolved nature of how variable limits are interpreted in plotting functions.

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?
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
7
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K