Integrating Norm of vectors - vector

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]])
*)

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!

geom_function with user written function in ggplot

I will be highly grateful for your help. I am learning how to use geom_function in r. Following is my function:
x0 <- 0.5
x1 <- 1
x2 <- 2
x3 <- 3
x <- c(x0, x1, x2, x3)
myfn <- function(w, b, a, x){
w^(1-b)/(1-b)-a*w-(w>100 & w<=200)*x[3]*(w-100)-(w>200)*x[3]*100-x[4]*(w-200)
}
My objective is to plot above function using geom_function to see how this function behaves with different values of arguments a and b and following is my code:
y=seq(0,1000,5)
ggplot()+
xlim(c(0,1000))+
geom_function(fun=myfn(w=y, b=-4, a=0.5, x=x))
Problem: I feel my logic is correct but when I execute above code, I get nothing. I will be highly grateful for the help. Thank you very much for the help in advance. Any help or direction will be highly appreciated.
Your function myfn is a function of w where a, b and x are parameters. To plot this function over the range of c(0, 1000) pass your function to the fun argument and the parameters as a list via the args argument and set the range via xlim:
x0 <- 0.5
x1 <- 1
x2 <- 2
x3 <- 3
x <- c(x0, x1, x2, x3)
myfn <- function(w, b, a, x) {
w^(1 - b) / (1 - b) - a * w - (w > 100 & w <= 200) * x[3] * (w - 100) - (w > 200) * x[3] * 100 - x[4] * (w - 200)
}
library(ggplot2)
ggplot() +
xlim(c(0, 1000)) +
geom_function(fun = myfn, args = list(b = -4, a = 0.5, x = x))
A second option would be to make use of a lambda function like so:
ggplot() +
xlim(c(0, 1000)) +
geom_function(fun = ~ myfn(.x, b = -4, a = 0.5, x = x))
myfn <- function(x, a, b, c) {
x^(1 - b) / (1 - b) - a * x - (x > 100 & x <= 200) * c[3] * (x - 100) - (x > 200) * c[3] * 100 - c[4] * (x - 200) # outcome is y
}
ggplot() +
xlim(c(0, 1000)) +
geom_function(fun = ~ myfn(x = .x, a = 0.5, b = -4, c = c(0.5, 1, 2, 3)))
If you do not want to add the variables through the args list you can add the variables to your function like this. Note I changed some of the variable names to make it more clear what the actual x and y are in the plot. Also x by the OP is just a list with 4 constants, I provided them as a and b under the name c.

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]]

Highlighting intersection points of 2 functions (Mathematica)

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]}]

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.)

Resources