What value is returned by the call f(4,2)? - recursion

//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 :-)

Related

Cannot get SageMath to solve simultaneous equations

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]

Algorithm to find the number of ways a number can be written as a sum of two or more positive numbers

It's a homework question and has to be solved using Dyanmic Programming approach.
What I've managed to do so far is that:
Let f(x) denote the number of times x can be written:
Then f(x) = f(x - 1) + 1 ; f(5) = f(4) + 1 (5 = 4 + 1)
But I don't think this is the right approach. Anybody would like to help?
An example of what the problem really is:
Number of ways 4 can be written:
4: 3 + 1
4: (2 + 1) + 1
4: 2 + 2
4: (1 + 1) + (1 + 1)
this representation is call partition. it could be solved in different ways.
for example, let's say
f(x, m) - number of partitions of x
such that the largest number in that partition is m
then
f(x, m) = sum of all f(x - m, k) where (1 <= k <= m),
also (k<=x-m), because f(x, y) = 0 where (y > x)
for your example ( let's count the number itself a partition also (f(x, x) = 1))
f(1, 1) = 1
f(2, 1) = f(1, 1) = 1
f(2, 2) = 1
f(3, 1) = f(2, 1) = 1
f(3, 2) = f(1, 1) = 1 //+ f(1, 2) zero
f(4, 1) = f(3, 1) = 1
f(4, 2) = f(2, 1) + f(2, 2) = 2
f(4, 3) = f(1, 1) = 1 // + f(1, 2) + f(1, 3) zeroes
f(4, 4) = 1
so the sum of f(4, 1), f(4, 2), f(4, 3), f(4, 4) = 5 ( 4 if not count 4 itself a partition)

Proving a tricky Recurrence Relation for the k + 1 case

I am absolutely stumped on this one.
T(n) = { 3, if n = 2 || T(n - 1) + (n/4), if n > 2
Prove by induction that T(n) = (n^2 + n + 18) / 8 [V n >= 2]
I know how to execute a proof by induction, but for some reason I cannot solve this expression for the k + 1 case.
Any help would be most excellent.
First, we choose n = 2:
T(n = 2) = (2^2 + 2 + 18) / 8 = 24 / 8 = 3
Great, that works. Now we know that there is a number k >= 2 that fulfills the definition of T(n). Then, let's set n = k+1 for an arbitrary k >= 2:
T(n = k+1) = ((k+1)^2 + k + 1 + 18) / 8
= (k^2 + 2k + 1 + k + 1 + 18) / 8
= (k^2 + k + 18) / 8 + (2 + 2k) / 8
= T(k) + k/4
That is exactly what the definition of T(n) says.
∎

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

Resources