Mathematica: How to use a single equation with multiple parameters to calculate any parameter - wolfram-mathematica

Currently I use a single equation with different combination of known/unknown parameters. As I don't have any fancy calculator it would be much easier to define the equation in Mathematica and passing known parameters to calculate unknown values.
I would be very thankful if anyone of you could give an example solution (if possible using given equation).
Let's say we have an equation of satellite speed at given point in the elliptical orbit:
v = sqrt(u(2/r - 1/a))
where
v = speed
u = constant 3.986 * 10^14 m^3/s^2
r = radius (distance from the center of the Earth)
a = semi major axis of the ellipse
This equation can be used to calculate the speed or for example we know what is the speed needed for a manoeuvre to move the cargo to other orbit and have to model the orbit (a) at given radius (r)
Thanks!

You can define equations in Mathematica using the ":=" operator. To define the example equation:
v[u_, r_, a_] := Sqrt[u*(2/r-1/a)]
I'm not sure how to generalize it to solve for any unknown...If I figure it out I'll get back to you.
You may want to try something like:
Solve[v[1, r, 7]==15, r]
that will solve for r assuming you know v, u, and a... you can then change each of the paramaters for the unknown...

A little bit late :) ... but Reduce[] does what you want. We define a function:
solveForMe[rules_] := Reduce[( v == Sqrt[3.986*10^14 *(2/r - 1/a)]) /. rules];
and invoke it with any valid combination for the assignments. For example:
In[72]:= Off[Reduce::ratnz];
solveForMe[{a -> 7 10^6, r -> 7 10^6}]
solveForMe[{v -> 10, r -> 7 10^6}]
solveForMe[{v -> 10, a -> 7 10^6}]
The output is :
Out[73]= v == 7546.05
Out[74]= a == 3.5*10^6
Out[75]= r == 1.4*10^7
HTH! ...

Related

Why is Mathematica producing a seemingly wrong answer for a derivative?

I'm puzzled by what I think is a mistake in a partial derivative I'm having Mathematica do for me.
Specifically, this is what I have:
Derivative I'd like to take
I'm trying to take the partial derivative of the following w.r.t. the variable θ (apologies for the formatting):
f=(1/4)(-4e((1+θ)/2)ψ+eN((1+θ)/2)ψ+eN((1+θ)/2-θd)ψ)-s
But the solution Mathematica produces seems very different from the one I get when I take the derivative myself. While Mathematica says the partial derivative of f w.r.t. θ is:
(1/4)eψ(N-2)
By hand, I get and am quite confident the correct answer is instead:
(1/4)eψ(N(1-d)-2)
That is, Mathematica is producing something that drops the variable d when it is differentiating. I've explored different functions that take a derivative in Mathematica, and the possibility that maybe some of the variables I'm using (such as d) might be protected or otherwise special, but I can't say that I know why the answer's so off. This is the first time in the notebook that d appears, so it is not set to 0. For context, I'm trying to confirm that the derivative of the function is positive for values of the variables in certain ranges, and we have d>0 and d<(1/2). Doing this all by hand works but I'm trying to confirm with Mathematica as I will be dealing with more complicated functions and need to make sure I'm having Mathematica produce the right derivatives.
Your didn't add spaces in eN and θd, so it thinks they're some other 2-character variables.
Adding spaces between them gives your expected result:
f[θ,e,N,ψ,d,s] = (1/4) (-4 e ((1+θ)/2) ψ + e N ((1+θ)/2) ψ + e N ((1+θ)/2 - θ d) ψ) - s;
D[f[θ, e, N, ψ, d, s], θ] // FullSimplify
(* 1/4 e (-2 + N - d N) ψ *)

Mathematica Does No Find the Global Maximum It Just Print Out My Function

I am am mathmatica notebook to find an analytical solution to the follow constrained optimization problem:
Max y^(1-b)(x^b(1-a(x/(x+1)))) s.t. M = Px+qy
x,y
I have tried the following code:
Maximize[{y^(1-b)(x^b(1-a(x/(x+1)))), M==Px+qy}, {x,y}]
and in returns the same function as an output. In the function a, b, M, P, and q are all parameters. I have also tried assigning the parameters arbitrary values to test to see if mathmatica is not sure how to deal with the parameters. I used to following code:
Maximize[{y^(1-0.5)(x^0.5(1-0.75(x/(x+1)))), 1000=5x+5y},{x,y}]
and it returns the same function. However, if I remove the constraint it will solve the optimization problem.
Maximize[{y^(1-0.5)(x^0.5(1-0.75(x/(x+1))))},{x,y}]
{7.2912*^59,{x->2.89727*^60,y->2.93582*^60}}
I am not sure what to do. After reading about constrained optimization problem the syntax appears to be correct. Sorry, it this question is really basic I am very new to mathmatica, also since I am using a notebook I could not past the output from the first two lines in.
The constraint is incorrectly specified, it should be 1000 == 5 x + 5 y. Maximize works better with exact numbers.
Maximize[{Rationalize[y^(1 - 0.5) (x^0.5 (1 - 0.75 (x/(x + 1))))],
1000 == 5 x + 5 y}, {x, y}] // N
(* {25.7537, {x -> 96.97, y -> 103.03}} *)

Mathematica transformation rules with trig functions

I'm an occasional Mathematica user and I am trying to transform an expression from spherical to Cartesian coordinates.
My function is defined as:
g[theta_, phi_] := Cos[phi](Sin[theta])^2 Sin[phi]
I'm hoping to transform that function using the following rules:
Sin[theta]Sin[phi] -> x
Cos[theta]-> y
Sin[theta]Cos[phi]-> z
in order to get the result:
zx
Here is the code I'm using to do that:
g[theta, phi] //. {Sin[theta]Sin[phi] -> x, Cos[theta] -> y, Sin[theta] Cos[phi] -> z}
And the result I get is:
Cos[phi] Sin[phi] Sin[theta]^2
So no transformation occurred.
Is there a function or an option I could add to help Mathematica figure out that the transformation is possible?
Thanks!
Perhaps this will be sufficient
Assuming[Sin[theta]Sin[phi]==x&&Cos[theta]==y&&Sin[theta]Cos[phi]==z,
Simplify[Cos[phi]Sin[theta]^2 Sin[phi]]]
which instantly returns
x z
That doesn't show you the steps or rules it used to arrive at that result, but because it considered x z to be "simpler" than your trig expression the evaluation process went in that direction.
There is a slightly more compact way of doing the same thing, if that matters. Simplify can accept a second argument which are the things which are assumed to be true during the process of simplification. Thus
Simplify[Cos[phi]Sin[theta]^2 Sin[phi],
Sin[theta]Sin[phi]==x&&Cos[theta]==y&&Sin[theta]Cos[phi]==z]
will give you exactly the same result

How to rearrange a function y = f[x] into x = g[y]

I have a differential equation A*dx/dt + B(y-y0) = 0
Where x is a very complicated function of y.
How can I use Mathematica to rearrange y to get a function x in order to solve this?
Thanks
There are two or three different problems here that you might be asking:
Option 1: The subject line
First, if you really do have a function f[x] defined and you want to rearrange it, you would be doing something like this:
f[x_]=2+x+x^2;
Solve[y==f[x],x]
However, even here you should notice that inverse functions are not necessarily unique. There are two functions given, and the domain of each is only for y>=7/4.
Option 2: Solving a DE
Now, the equation you give is a differential equation. That is not the same as "rearranging a function y=f[x] into x=g[y]" because there are derivatives involved.
Mathematica has a built-in differential-equation solver:
DSolve[a y'[t] + b (y[t] - y0) == 0, y[t], t]
That will give you a function (in terms of constants $a,b,y_0$) that is the answer, and it will include the unspecified constant of integration.
Your system seems to refer to two functions, x(t) and y(t). You cannot solve one equation for two variables, so it is impossible to solve this (Mathematica or otherwise) without more information.
Option 3: Rearranging an expression
As a third alternative, if you are trying to rearrange this equation without solving the differential equation, you can do that:
Solve[a x'[t] + b(y[t]-y0)==0,x'[t]]
This will give you $x'(t)$ in terms of the other constants and the function $y(t)$, but in order to integrate this (i.e. to solve the differential equation) you will need to know more about y[t].

Best way to do an iteration scheme

I hope this hasn't been asked before, if so I apologize.
EDIT: For clarity, the following notation will be used: boldface uppercase for matrices, boldface lowercase for vectors, and italics for scalars.
Suppose x0 is a vector, A and B are matrix functions, and f is a vector function.
I'm looking for the best way to do the following iteration scheme in Mathematica:
A0 = A(x0), B0=B(x0), f0 = f(x0)
x1 = Inverse(A0)(B0.x0 + f0)
A1 = A(x1), B1=B(x1), f1 = f(x1)
x2 = Inverse(A1)(B1.x1 + f1)
...
I know that a for-loop can do the trick, but I'm not quite familiar with Mathematica, and I'm concerned that this is the most efficient way to do it. This is a justified concern as I would like to define a function u(N):=xNand use it in further calculations.
I guess my questions are:
What's the most efficient way to program the scheme?
Is RecurrenceTable a way to go?
EDIT
It was a bit more complicated than I tought. I'm providing more details in order to obtain a more thorough response.
Before doing the recurrence, I'm having problems understanding how to program the functions A, B and f.
Matrices A and B are functions of the time step dt = 1/T and the space step dx = 1/M, where T and M are the number of points in the {0 < x < 1, 0 < t} region. This is also true for vector the function f.
The dependance of A, B and f on x is rather tricky:
A and B are upper and lower triangular matrices (like a tridiagonal matrix; I suppose we can call them multidiagonal), with defined constant values on their diagonals.
Given a point 0 < xs < 1, I need to determine it's representative xn in the mesh (the closest), and then substitute the nth row of A and B with the function v( x) (transposed, of course), and the nth row of f with the function w( x).
Summarizing, A = A(dt, dx, xs, x). The same is true for B and f.
Then I need do the loop mentioned above, to define u( x) = step[T].
Hope I've explained myself.
I'm not sure if it's the best method, but I'd just use plain old memoization. You can represent an individual step as
xstep[x_] := Inverse[A[x]](B[x].x + f[x])
and then
u[0] = x0
u[n_] := u[n] = xstep[u[n-1]]
If you know how many values you need in advance, and it's advantageous to precompute them all for some reason (e.g. you want to open a file, use its contents to calculate xN, and then free the memory), you could use NestList. Instead of the previous two lines, you'd do
xlist = NestList[xstep, x0, 10];
u[n_] := xlist[[n]]
This will break if n > 10, of course (obviously, change 10 to suit your actual requirements).
Of course, it may be worth looking at your specific functions to see if you can make some algebraic simplifications.
I would probably write a function that accepts A0, B0, x0, and f0, and then returns A1, B1, x1, and f1 - say
step[A0_?MatrixQ, B0_?MatrixQ, x0_?VectorQ, f0_?VectorQ] := Module[...]
I would then Nest that function. It's hard to be more precise without more precise information.
Also, if your procedure is numerical, then you certainly don't want to compute Inverse[A0], as this is not a numerically stable operation. Rather, you should write
A0.x1 == B0.x0+f0
and then use a numerically stable solver to find x1. Of course, Mathematica's LinearSolve provides such an algorithm.

Resources