Highlighting intersection points of 2 functions (Mathematica) - plot

Given two functions, I need to find their intersection points and show them on the graph. For this particular problem, the functions are: f(x) = - (x - 2) ^ 2, g(x) = x/(x+1).
So far, I have the following:
Plot[{-(x - 2)^2 + 4, x/(x + 1)}, {x, 0, 4}, Filling -> {1 -> {{2}, {White, LightBlue}}}]
NSolve[-(x - 2)^2 + 4 == x/(x + 1), {x, y}]
But I have no idea how to show the points on a graph. How do I do that?

You can use the Epilog option to add graphics primitives to a plot:
intersections = {x, y} /.
NSolve[y == -(x - 2)^2 + 4 && y == x/(x + 1), {x, y}];
Plot[{-(x - 2)^2 + 4, x/(x + 1)}, {x, 0, 4},
Filling -> {1 -> {{2}, {White, LightBlue}}},
Epilog -> {Red, Point[intersections]}]

Related

Different colors plotting fixed points in NSolve

I am plotting fixed points of a system of differential equations in terms of a parameter. The code is
PX1Y1 = (1 - s1) (y1 + y2);
PX1Y2 = 0;
PX1X2 = 0;
PX2Y1 = 0;
PX2Y2 = s2 (y1 + y2);
PX2X1 = 0;
PY1X1 = s1 (x1 + x2);
PY1Y2 = 0;
PY1X2 = 0;
PY2X1 = 0;
PY2X2 = (1 - s2) (x1 + x2);
PY2Y1 = 0;
x1eq = -x1 (PX1Y1 + PX1Y2 + PX1X2) + x2 PX2X1 + y1 PY1X1 + y2 PY2X1;
x2eq = -x2 (PX2Y1 + PX2Y2 + PX2X1) + x1 PX1X2 + y1 PY1X2 + y2 PY2X2;
y1eq = -y1 (PY1X1 + PY1Y2 + PY1X2) + x1 PX1Y1 + x2 PX2Y1 + y2 PY2Y1;
y2eq = -y2 (PY2X1 + PY2X2 + PY2Y1) + x1 PX1Y2 + x2 PX2Y2 + y1 PY1Y2;
Xsimp[x1_, x2_, y1_, y2_] = Simplify[x1eq + x2eq]
Xeq = Simplify[
Xsimp[X/2 + \[Omega]/2,
X/2 - \[Omega]/2, (1 - X)/2 - (\[Omega] - \[Gamma])/2, (1 - X)/
2 + (\[Omega] - \[Gamma])/2]]
\[Omega]simp[x1_, x2_, y1_, y2_] = Simplify[x1eq - x2eq];
\[Omega]eq =
Simplify[\[Omega]simp[X/2 + \[Omega]/2,
X/2 - \[Omega]/2, (1 - X)/2 - (\[Omega] - \[Gamma])/2, (1 - X)/
2 + (\[Omega] - \[Gamma])/2]]
Manipulate[
Row[{
paramsplot = {s1 -> a, s2 -> b};
sol = NSolve[
{Xeq == 0, \[Omega]eq == 0} /. paramsplot,
{X, \[Omega]}, Reals
];
Plot[
X /. sol, {\[Gamma], -1, 1},
AxesLabel -> {"\[Gamma]", "X fixed points"},
ImageSize-> 300,
PlotRange -> {{-1, 1}, {0, 1}}
],
Plot[
\[Omega] /. sol, {\[Gamma], -1, 1},
AxesLabel -> {"\[Gamma]", "\[Omega] fixed points"},
ImageSize -> 300,
PlotRange -> {{-1, 1}, {-1, 1}}]
}],
{{a, 0.6,"s1 (0, 1)"}, 0, 1},
{{b, 0.4, "s2 (0, 1)"}, 0, 1}
]
And I get this
Is there any way of plotting each fixed point in a different colour? In the sense that what is green, so to say, in the X graph corresponds to what is green in the omega graph, and so on.
Thank you!

Returning Powers of a Two-Variable Polynomial as a Vector in PARI/GP?

I'm trying to view the monomials of a two-variable polynomial in vector form. So, for example, if I input x^2 + x^3*y + x*y + y^2 + 1, I would like to view it as [[2;0], [3;1], [1;1], [0;2], [0;0]] - a vector made of column vectors.
If I use Vec on a two-variable polynomial, it simply treats the second variable as a number, giving Vec( x^2 + x^3*y + x*y + y^2 + 1 ) = [ y, 1, y, y^2 + 1 ], which I don't think can then be twisted into something that would work for me.
Any ideas on how this might be able to be done?
You have to calc all the monomials by your own. Your bivariate polynomial can be viewed as the univariate polynomial having polynomial coefficients. Firstly, you select the primary variable (e.g. 'x) and find its exponents together with its nonzero coefficients:
exponents(f, v=variable(f)) = {
if(type(f) != "t_POL",
return([[0, f]])
);
my(x = varhigher("#"));
my(coeffs = Vecrev(subst(f, v, x)));
my(indexes = select((c) -> c != 0, coeffs, 1));
[[n-1, coeffs[n]] | n <- Vec(indexes)]
};
exponents(1)
> [[0, 1]]
exponents(x^2 + x^3*y + x*y + y^2 + 1)
> [[0, y^2 + 1], [1, y], [2, 1], [3, y]]
exponents(x^2 + x^3*y + x*y + y^2 + 1, 'y)
> [[0, x^2 + 1], [1, x^3 + x], [2, 1]]
Secondly, given such list of exponents and coefficients you easily obtain the complete list of monomials:
monomial_list(f) = {
concat(
apply(
(xs) -> [[xs[1], p[1]] | p <- exponents(xs[2])],
exponents(f)
)
)
};
monomial_list(0)
> [[0, 0]]
monomial_list(x^2 + x^3*y + x*y + y^2 + 1)
> [[0, 0], [0, 2], [1, 1], [2, 0], [3, 1]]

Integrating Norm of vectors

I have two vectors which I want to integrate in Matematica. Let the vectors be
r = {x, y};
Q = {x1, y1};
then I write this command
Integrate[
1/Norm[-((a*Q)/c) + r],
{a, 0, 1},
Assumptions -> (a*x1)/c > x && x ->
Real && (a*x1)/c ->
Real && x > 0 && (a*y1)/c -> Real && (a*y1)/c > y && y > 0
]
Where c is a positive constant. The output yields the same
Integrate[1/Norm[-((a Q)/c) + r], {a, 0, 1},
Assumptions -> (a x1)/c > 0 && (a x1)/c > x && x ->
Real && (a x1)/c -> Real && x > 0 && (a y1)/c > y && y > 0]
Could you please tell me where I am making a mistake?
I would be grateful if you could help me,
Thanks
r = {x, y};
Q = {x1, y1};
Integrate[1/Sqrt[(-((a*Q)/c) + r).(-((a*Q)/c) + r)], {a, 0, 1},
Assumptions -> Element[{x, y, x1, y1, a, c}, Reals]]
Returns:
(*
(1/Sqrt[x1^2 + y1^2])c (-Log[c (-x x1 - y y1 +Sqrt[(x^2 + y^2) (x1^2 + y1^2)])]+
Log[x1^2 + y1^2 - c (x x1 + y y1) +
(c Sqrt[(x1^2 + y1^2) (x1^2 + c^2 (x^2 + y^2) + y1^2 - 2 c (x x1 + y y1))])/
Abs[c]])
*)

2 Dimension Runge-Kutta Method on Mathematica 8

I have a problem while programing in Mathematica 8, here is my code:
f[t_, y_] := {y, y};
RungeKutta3[a_, b_, Alpha_, n_, f_] :=
Module[{h, j, k1, k2, k3},
h = (b - a)/n;
Y = T = Table[0, {100 + 1}];
Y[[1]] = Alpha;
T[[1]] = a;
For[j = 1, j <= n, ++j,
k1 = f[T[[j]], Y[[j]]];
k2 = f[T[[j]] + h/2, Y[[j]] + k1*h/2];
k3 = f[T[[j]] + h, Y[[j]] + (-k1 + 2 k2)h];
Y[[j + 1]] = Y[[j]] + h/6(k1 + 4 k2 + k3);
(* Print[j, "----->", Y[[j]]];*)
T[[j + 1]] = T[[j]] + h;
];];
RungeKutta3[0., 1., {300., 500}, 2, f];
The thing is, I'm trying to implement a Runge-Kutta method. And I was successful actually, but only with a function f[x_] that had 1 dimension. This code is for 2 dimensions, but it simply doesn't work and I don't know why. Here is an example for a code with 1 dimension only (notice that I have to change the first line to define the function and the last line, when I call "RungeKutta3").
f[t_, y_] := y;
RungeKutta3[a_, b_, Alpha_, n_, f_] :=
Module[{h, j, k1, k2, k3},
h = (b - a)/n;
Y = T = Table[0, {100 + 1}];
Y[[1]] = Alpha;
T[[1]] = a;
For[j = 1, j <= n, ++j,
k1 = f[T[[j]], Y[[j]]];
k2 = f[T[[j]] + h/2, Y[[j]] + k1*h/2];
k3 = f[T[[j]] + h, Y[[j]] + (-k1 + 2 k2)*h];
Y[[j + 1]] = Y[[j]] + h/6*(k1 + 4 k2 + k3);
(* Print[j, "----->", Y[[j]]];*)
T[[j + 1]] = T[[j]] + h;
];];
RungeKutta3[0., 1., 300., 100, f];
To sum up, how do I implemented the Runge-Kutta method for a function with 2 dimensions??
If you could help me out I would be grateful.
Thanks in advance!
PS: the Runge-Kutta method is order 3
----------------------
Problem solved! Check the code, if anybody needs help with anything, just ask!
f[t_, y1_, y2_] := 3 t*y2 + Log[y1] + 4 y1 - 2 t^2 * y1 - Log[t^2 + 1] - t^2;
F[t_, {y1_, y2_}] := {y2, f[t, y1, y2]};
RungeKutta3[a_, b_, [Alpha]_, n_, f_] :=
Module[{h, j, k1, k2, k3, Y, T, R},
h = (b - a)/n;
Y = T = Table[0, {n + 1}];
Y[[1]] = [Alpha]; T[[1]] = a;
For[j = 1, j <= n, ++j,
k1 = f[T[[j]], Y[[j]]];
k2 = f[T[[j]] + h/2, Y[[j]] + k1*h/2];
k3 = f[T[[j]] + h, Y[[j]] + (-k1 + 2 k2)*h];
Y[[j + 1]] = Y[[j]] + h/6*(k1 + 4 k2 + k3);
T[[j + 1]] = T[[j]] + h;
];
R = Table[0, {n + 1}];
For[j = 1, j <= n + 1, j++, R[[j]] = Y[[j]][[1]]];
Print[ListPlot[Transpose[{T, R}]]]
];
RungeKutta3[0., 1, {1., 0.}, 1000, F];
I know basically have a mathematica program that can solve ANY 2nd order equation! Through Runge-Kutta method. just insert your function on
f[t_, y1_, y2_]:= [Insert your function here]
where t is the independent value, y1 is the function itself y(t), y2 is y'(t).
Call the function through:
RungeKutta3[a, b, [Alpha], n, F];
where a is the initial "t" value, b the final "t" value, [Alpha] the initial value of your function and the first derivative (given in the form {y1(a),y2(a0)}), n the number of points equally spaced you want to represent. F is the function you have to insert despite of the function you give to f
Any questions feel free to ask!!
PS: The Runge-Kutta problem solves differential equations with problems of initial values, i used this program as a base to solve a problem of boundary values, if you want it just text me!
Doesn't your code just implement what is already built into Mathematica, namely, if you were to use the option
Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 3}
to NDSolve?
(This is not to suggest there's no value in "rolling your own": perhaps you want to do it as a learning exercise for yourself or for students, or as a student yourself.)

How to calculate number of rectangles in rectangular grid?

I want to calculate number of rectangles in a rectangles.It can be done using formula
(x^2+x)(y^2+y)/4
but it also includes perfect squares like 1*1,2*2,3*3 etc.I dont want to include that in my calculations.How can i do that?
Ok, you have the number of rectangles with integer coordinates between the points (0, 0), (x, 0), (x, y) and (0, y), x and y being integers too. You now need to remove the perfect squares from this sum.
To compute it, let's evaluate the number of squares 1*1: there are obviously x*y of them. For squares 2*2, we have x-1 choices for the x-coordinate and y-1 for the y-coordinate of the bottom left-hand corner of such a square, due to the width of this square: this results in (x-1)*(y-1) squares. Idem, we will have (x-2)*(y-2) squares 3*3, etc.
So for a given i, we have (x - i + 1) * (y - i + 1) squares i*i, and i goes from 1 to the minimum of x and y (of course if x is 4 and y is 7, we cannot have a square with a side greater than 4).
So if m = min(x, y), we have:
Sum_Squares = Sum(i = 1, i = m, (x - i + 1) * (y - i + 1))
= Sum(j = 0, j = m - 1, (x - i) * (y - i))
= Sum(j = 0, j = m - 1, x*y - (x+y)*j + j^2)
= m*x*y - (x+y)*Sum(j = 0, j = m - 1, j) + Sum(j = 0, j = m - 1, j^2)
= m*x*y - (x+y)*Sum(j = 1, j = m - 1, j) + Sum(j = 1, j = m - 1, j^2)
= m*x*y - (x+y)*m*(m-1)/2 + (m-1)*m*(2*m-1)/6
I get that with an index change (j = i - 1) and via the well-known formulas:
Sum(i = 1, i = n, j) = n*(n + 1)/2
Sum(i = 1, i = n, j^2) = n*(n + 1)*(2*n + 1)/6
You just have to remove this Sum_Squares from (x^2+x)(y^2+y)/4 and you're done !

Resources