I will provide some context on the problem if necessary. I've thought about this problem, but cannot seem to see why SageMath wont solve it. I am asking this question here as I think it has more a place here than the mathematics StackExchange.
When I run this code, it just returns/shows the equations I have put in. I know there is a solution to these equations, but I have no idea why it wont solve them.
Even running this simple code gives the same output:
var('a b c d e f g h i')
equations=solve([a*b+a*c+b*c==0,c*d+a*e+b*f==1,c*g+a*h+b*i==0,b*d+c*e+a*f==0,d*e+d*f+e*f==0,f*g+d*h+e*i==1,b*g+c*h+a*i==1,e*g+f*h+d*i==0,g*h+g*i+h*i==0],a,b,c,d,e,f,g,h,i)
show(equations)
Output:
[a*b + a*c + b*c == 0, b*d + c*e + a*f == 0, c*d + a*e + b*f == 1, d*e + d*f + e*f == 0, b*g + c*h + a*i == 1, c*g + a*h + b*i == 0, e*g + f*h + d*i == 0, f*g + d*h + e*i == 1, g*h + g*i + h*i == 0]
Related
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]]
Using ggplot I want to plot straight lines of the form ax + by + c = 0, where the three parameters are generated by my code. If neither a nor b are zero, ggplot requires me to use geom_abline but if a = 0 or b = 0, I'd need to use geom_vline or geom_hline respectively.
It is messy to have to test a=0 and b=0 to select the right geom. What I'd really like is a geom like this:
geom_line(x_param = a, y_param = b, const = c)
but it does not seem to exist.
Is there a non-messy solution to my problem?
It's difficult to help without some example data, but suppose you had parameters in a data frame like this:
df.params <- data.frame(
a = c(0, 0, 1, .3),
b = c(1, .27, 0, 0),
c = c(1, 2, 3, 4)
)
a b c
1 0.0 1.00 1
2 0.0 0.27 2
3 1.0 0.00 3
4 0.3 0.00 4
From this, you could use a single call to geom_segment to draw vertical or horizontal lines using Inf values. You can use ifelse to test for zeros in your parameters and swap in either Infs or your intercept values for each line:
ggplot(data = df.params) +
geom_segment(aes(
x = ifelse(a == 0, -Inf, c),
xend = ifelse(a == 0, Inf, c),
y = ifelse(b == 0, -Inf, c),
yend = ifelse(b == 0, Inf, c)
)
)
For the record, here is the wrapper function that I wrote follwoing the very helpful comments given. In my opinion it is not too inelegant.
line <- function(a, b, c) {
if (a == 0 & b == 0) {
warning('Invalid parameters for line')
} else if (b == 0) {
geom <- geom_vline(xintercept = -c / a)
} else {
geom <- geom_abline(intercept = -c / b, slope = -a / b)
}
return(geom)
}
//pseudocode
//n and k are nonnegative integers`
int f(int n, int k)`
`if(k*n ==0)`
`return 1`
`else`
`return f(n-1,k-1)+f(n-1,k)`
`end if`
end f
What I have so far is this. But how do I find the value of this function call?
f(3,1) + f(3,2)
f(2,0) +f(2,1) f(2,1)+f(2,2)
1 f(1,0)+f(1,1) f(1,1)+f(1,2)
f(0,0)+f(0,1) f(0,1)+f(0,2)
Basically the answer is just continuing what you started:
f(4, 2) ; ==
f(3, 1) + f(3, 2) ; ==
f(2, 0) + f(2, 1) + f(2, 1) + f(2, 2) ; ==
1 + f(1, 0) + f(1, 1) + f(1, 0) + f(1, 1) + f(1, 1) + f(1, 2) ; ==
1 + 1 + f(0, 0) + f(0, 1) + 1 + f(0, 0) + f(0, 1)+ f(0, 0) +
f(0, 1) + f(0, 1) + f(0, 2) ; ==
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ; ==
11
The answer is 11.
One simple way to understand recursion function call is to expand the recursion call stack tree, which can help you clearly see how the whole process goes(Hope the pic is clear enough):
In the comment I mentioned divede-and-conquer process in a recursion process, so I updated a new image to contain the whole process. Hope this could be helpful :-)
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]}]
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.)