Approximating gradient using python - python-3.x

I have attempted to solve the following problem.
For the calculation of gradient we are obliged to use an approximate calculation:
I tried to solve it for each vector e of the canonical basis of R^4 and using h = 1e-05 for example.
However, I made an example for R^2, but I'm not sure if my code is correct for this case and I need to change code for the formule in the picture.
def f(x,y):
return np.sin(x)+np.cos(y)
def derivative(func, vx, h):
e = np.array([[1,0],[0,1]]) #Basis canonique of R^2
x = vx[0]
y = vx[1]
dx=(func(x + e[0]*h,y) - func(x,y)) / h #directional derivative in x
dy=(func(x ,y+e[1]*h) - func(x,y)) / h #directional derivative in y
grad = np.array([dx[0],dy[1]])
return grad
vx=np.array([np.pi,1])
derivative(f,vx,h)
Results of this code:
In [150]: derivative(f,vx,h)
Out[150]: array([-1. , -0.84147369])
I am a little confused how to do this problem but I was hoping to get some help with fixing the code I produced so far. Thanks!

Review section 4.6 - Systems of Equations of the text below:
Numerical Methods in Engineering with Python 3 (3rd ed.)

Related

Is there any method/solver in python to solve embedded derivatives in a ODE equation?

I've got this equation from mathematical model to know the thermal behavior of a battery.
dTsdt = Ts * a+ Ta * b + dTadt * c + d
However, i can't get to solve it due to the nested derivatives.
I need to solve the equation for Ts and Ta.
I tried to define it as follows, but python does not like it and several eŕrors show up.
Im using scipy.integrate and the solver ODEint
Since the model takes data from vectors, it has to be solved for every time step and record the output accordingly.
I also tried assinging the derivatives to a variable v1,v2, and then put everything in an equation without derivatives like the second approach shown as follows.
def Tmodel(z,t,a,b,c,d):
    Ts,Ta= z
    dTsdt = Ts*a+ Ta*b + dTadt*c+ d
    dzdt=[dTsdt]
    return dzdt
z0=[0,0]
# solve ODE
for i in range(0,n-1):
   
    tspan = [t[i],t[i+1]]
    # solve for next step
    z = odeint(Tmodel,z0,tspan,arg=(a[i],b[i],c[i],d[i],))
    # store solution for plotting
    Ts[i] = z[1][0]
    Ta[i] = z[1][1]
    # next initial condition
    z0 = z[1]
def Tmodel(z,t,a,b,c,d):
    Ts,v1,Ta,v2= z
# v1= dTsdt
# v2= dTadt
    v1 = Ts*a+ Ta*b + v2*c+ d
    dzdt=[v1,v2]
    return dzdt
That did not work either.I believe there might be a solver capable of solving that equation or the equation must be decouple in a way and solve accordingly.
Any advice on how to solve such eqtn with python would be appreciate it.
Best regards,
MM
Your difficulty seems to be that you are given Ta in a form with no easy derivative, so you do not know where to take it from. One solution is to avoid this derivative completely and solve the system for y=Ts-c*Ta. Substitute Ts=y+c*Ta in the right side to get
dy/dt = y*a + Ta*(b+c*a) + d
Of course, this requires then a post-processing step Ts=y+c*Ta to get to the requested variable.
If Ta is given as function table, use an interpolation function to get values at any odd time t that is demanded by the ODE solver.
Ta_func = interp1d(Ta_times,Ta_values)
def Tmodel(y,t,a,b,c,d):
Ta= Ta_func(t)
dydt = y*a+ Ta*(b+c*a) + d
return dydt
y[0] = Ts0-c*Ta_func(t[0])
for i in range(len(t)-1):
y[i+1] = odeint(Tmodel,y[i],t[i:i+2],arg=(a[i],b[i],c[i],d[i],))[-1,0]
Ts = y + c*Ta_func(t)

Having issues solving pair of nonlinear equations using Python

So I know there have been plenty of questions/answers on this topic, but I haven't been able to locate exactly what is going wrong in my attempts. I have two nonlinear function f(x,y) and g(x,y) and I am trying to solve the system
f(x,y) - g(x,y) = 0
f(x,y) + g(x,y) = c
where c is some positive constant. I have been using the snippet described in the answer to this question: How to solve a pair of nonlinear equations using Python?, but I am facing issues. If I run that snippet for my code, it returns the x and y values such that only the second equation in the system is satisfied, i.e. it returns x and y such that f(x,y) + g(x,y) = c, while for the other equation it holds that f(x,y) - g(x,y) != 0. I get the exact same issues when using the scipy.optimize.root function. I'm quite lost as to what could be causing this issue. Could it mean that there do not exist x, y such that both equations are satisfied?
Thanks in advance for any help!
It is very possible that there is no solution. x + y = 10, x + y = 20 has no solution, for example. This isn't an issue of non-linearity; this is an issue of math. Also, it might be possible, if this can't be solved algebraically, that the first equation has f(x,y) - g(x,y) is approximately zero. If f(x,y)-g(x,y)=0.0001, would you consider this close enough?
For completeness: Check out the math, as noted by #tstanisl. If you add the equations together, you solve f(x,y)=c/2 or g(x,y)=c/2, which is easier.

Solving vector second order differential equation while indexing into an array

I'm attempting to solve the differential equation:
m(t) = M(x)x'' + C(x, x') + B x'
where x and x' are vectors with 2 entries representing the angles and angular velocity in a dynamical system. M(x) is a 2x2 matrix that is a function of the components of theta, C is a 2x1 vector that is a function of theta and theta' and B is a 2x2 matrix of constants. m(t) is a 2*1001 array containing the torques applied to each of the two joints at the 1001 time steps and I would like to calculate the evolution of the angles as a function of those 1001 time steps.
I've transformed it to standard form such that :
x'' = M(x)^-1 (m(t) - C(x, x') - B x')
Then substituting y_1 = x and y_2 = x' gives the first order linear system of equations:
y_2 = y_1'
y_2' = M(y_1)^-1 (m(t) - C(y_1, y_2) - B y_2)
(I've used theta and phi in my code for x and y)
def joint_angles(theta_array, t, torques, B):
phi_1 = np.array([theta_array[0], theta_array[1]])
phi_2 = np.array([theta_array[2], theta_array[3]])
def M_func(phi):
M = np.array([[a_1+2.*a_2*np.cos(phi[1]), a_3+a_2*np.cos(phi[1])],[a_3+a_2*np.cos(phi[1]), a_3]])
return np.linalg.inv(M)
def C_func(phi, phi_dot):
return a_2 * np.sin(phi[1]) * np.array([-phi_dot[1] * (2. * phi_dot[0] + phi_dot[1]), phi_dot[0]**2])
dphi_2dt = M_func(phi_1) # (torques[:, t] - C_func(phi_1, phi_2) - B # phi_2)
return dphi_2dt, phi_2
t = np.linspace(0,1,1001)
initial = theta_init[0], theta_init[1], dtheta_init[0], dtheta_init[1]
x = odeint(joint_angles, initial, t, args = (torque_array, B))
I get the error that I cannot index into torques using the t array, which makes perfect sense, however I am not sure how to have it use the current value of the torques at each time step.
I also tried putting odeint command in a for loop and only evaluating it at one time step at a time, using the solution of the function as the initial conditions for the next loop, however the function simply returned the initial conditions, meaning every loop was identical. This leads me to suspect I've made a mistake in my implementation of the standard form but I can't work out what it is. It would be preferable however to not have to call the odeint solver in a for loop every time, and rather do it all as one.
If helpful, my initial conditions and constant values are:
theta_init = np.array([10*np.pi/180, 143.54*np.pi/180])
dtheta_init = np.array([0, 0])
L_1 = 0.3
L_2 = 0.33
I_1 = 0.025
I_2 = 0.045
M_1 = 1.4
M_2 = 1.0
D_2 = 0.16
a_1 = I_1+I_2+M_2*(L_1**2)
a_2 = M_2*L_1*D_2
a_3 = I_2
Thanks for helping!
The solver uses an internal stepping that is problem adapted. The given time list is a list of points where the internal solution gets interpolated for output samples. The internal and external time lists are in no way related, the internal list only depends on the given tolerances.
There is no actual natural relation between array indices and sample times.
The translation of a given time into an index and construction of a sample value from the surrounding table entries is called interpolation (by a piecewise polynomial function).
Torque as a physical phenomenon is at least continuous, a piecewise linear interpolation is the easiest way to transform the given function value table into an actual continuous function. Of course one also needs the time array.
So use numpy.interp1d or the more advanced routines of scipy.interpolate to define the torque function that can be evaluated at arbitrary times as demanded by the solver and its integration method.

How do i Square Root a Function in VBA

I am working on a MonteCarlo simulation model and part of it is to calculate the following formula:
X = Sqr(1-p)Y + Sqr(p)Z,
Where:
Y and Z are randomly obtained values based (idiosyncratic and systematic factors, respectviely) on a standard normal (inv.) distribution, calculated as:
Application.WorksheetFunction.NormInv (Rnd(), mean, sd)
p represents a correlation factor.
My aim is to square root a recalled formula, however when I try the following (inserting the first Sqr), it does not work and gives an error:
Matrix (n, sims) = (R * Sqr(Application.WorksheetFunction.NormInv(Rnd(), mean, sd))) + (Sqr(1 - R) * RandomS(s, x))
where:
R: Correlation factor
RandomS(s,x): generated matrix with Z values.
I don't want to go into too much details about the background and other variables, as the only problem I am getting is with Square Rooting the equation.
Error message I recieve reads:
Run-time error '5':
Invalid procedure call or argument
When I click debug it takes me to the formula, therefore there must be something wrong with the syntax.
Can you help with directly squaring the formula?
Thank you!
Andrew
Square root is simply Sqr.
It works fine in Excel VBA, so for example:
MsgBox Sqr(144)
...returns 12.
Just don't confuse it with the syntax for a worksheet function with is SQRT.
If you're still having an issue with your formula, tit must be with something other than the Square Root function, and I'd suggest you check the values of your variable, and make sure they are properly declared (preferably with Option Explicit at the top of the module).
Also make sure that you're passing Sqr a positive number.
Documentation: Sqr Function
I'm not a math major, but with your formula:
X = Sqr(1-p)Y + Sqr(p)Z,
...you specified how Y and Z are calculated, so calculate them separately to keep it simple:
Dim X as Double, Y as Double, Z as Double
Y = Application.WorksheetFunction.NormInv (Rnd(), mean, sd)
Z = Application.WorksheetFunction.NormInv (Rnd(), mean, sd)
Assuming the comma is not supposed to be in the formula, and having no idea what p is, your final code to calculate X is:
X = Sqr(1-p) * Y + Sqr(p) * Z

How do I solve this exponential equation on Excel Solver?

100e^0.25*y = 97.5
Solving for y
Using Excel Solver
I tried using empty column entry for y in 'By changing cells' and Set objective function as LHS of above equation (empty column entry in equation included) equal to value of 97.5 in solver.
It gives no solution
How do I do this?
It's a bit ambiguous what you're asking...
Literal math interpretation: 100*(e^0.25)*y = 97.5
Then y = 97.5 / ( 100 * exp(.25)) = .759
My guess of what you want: 100*e^(0.25*y) = 97.5
Then y = ln(97.5/100) / .25 = -.101
Another possibility: (100 * e)^(0.25 * y) = 97.5
Then y = (ln(97.5) / ln(100*e)) / .25 = 3.268
Whatever it is, this doesn't need solver!
You don't really need the solver. Just re-arrange your formula to solve for Y. Since y = b^x is the same as log(b)Y = x (log of Y, with base b)
Your formula above is the same as:
Y = (log(100e)97.5))/.25
(Read aloud, that's log of 97.5, with base 100e, divided by .25
So, Y = 3.268305672
(Bonus points for someone who can tell me how to format this so the Log looks correct)
The question is "How do I solve this exponential equation on Excel Solver?" which is a fair enough question, as it points to trying to understand how to set up solver.
My interpretation of the equation provided is given in this screenshot ...
The solver dialog box is then setup as follows ...
Of note:
This is a non-linear equation and needs GRG Nonlinear. If you choose LP Simplex, it will not pass the linearity test.
Ensure "Make Unconstrained Variables Non-Negative" is not checked.
It provided this result for me ...
A more precise answer can be obtained by decreasing the "Convergence" value on the GRG Non-Linear Options dialog.
A problem this simple can also be solved using Goal Seek.

Resources