I have following anonymous function (with x as an array):
f = #(x) 312*x(2) - 240*x(1) + 30*x(3) - 24*x(4) + 282*x(1)*x(2) + 30*x(1)*x(3) + 18*x(1)*x(4) + 54*x(2)*x(3) + 6*x(2)*x(4) + 6*x(3)*x(4) + 638*x(1)^2 + 207*x(2)^2 + 6*x(3)^2 + 3*x(4)^2 + 4063
I want to make gradient of this function and save it for future use. Also with array input.
X = [ 0;...
0;...
0;...
0];
F = f(X)
G = g(X)
Is it possible to archive this with this type of function? Or maybe it is possible to somehow make it via diff command? Something like this:
g = [diff(f, x(1));...
diff(f, x(2));...
diff(f, x(3));...
diff(f, x(4))]
I guess the following is what you want. I'm afraid, you need the Symbolic Math Toolbox for a simple solution, otherwise I'd rather calculate the derivatives by hand.
x = [1 2 3 4];
%// define function
syms a b c d
f = 312*b - 240*a + 30*c - 24*d + 282*a*b + 30*a*c + 18*a*d + 54*b*c + ...
6*b*d + 6*c*d + 638*a^2 + 207*b^2 + 6*c^2 + 3*d^2 + 4063
%// symbolic gradient
g = gradient(f,[a,b,c,d])
%// eval symbolic function
F = subs(f,[a,b,c,d],x)
G = subs(g,[a,b,c,d],x)
%// convert symbolic value to double
Fd = double(F)
Gd = double(G)
or alternatively:
%// convert symbolic function to anonymous function
fd = matlabFunction(f)
gd = matlabFunction(g)
%// eval anonymous function
x = num2cell(x)
Fd = fd(x{:})
Gd = gd(x{:})
f =
638*a^2 + 282*a*b + 30*a*c + 18*a*d - 240*a + 207*b^2 + 54*b*c +
6*b*d + 312*b + 6*c^2 + 6*c*d + 30*c + 3*d^2 - 24*d + 4063
g =
1276*a + 282*b + 30*c + 18*d - 240
282*a + 414*b + 54*c + 6*d + 312
30*a + 54*b + 12*c + 6*d + 30
18*a + 6*b + 6*c + 6*d - 24
F =
7179
G =
1762
1608
228
48
fd =
#(a,b,c,d)a.*-2.4e2+b.*3.12e2+c.*3.0e1-d.*2.4e1+a.*b.*2.82e2+a.*c.*3.0e1+a.*d.*1.8e1+b.*c.*5.4e1+b.*d.*6.0+c.*d.*6.0+a.^2.*6.38e2+b.^2.*2.07e2+c.^2.*6.0+d.^2.*3.0+4.063e3
gd =
#(a,b,c,d)[a.*1.276e3+b.*2.82e2+c.*3.0e1+d.*1.8e1-2.4e2;a.*2.82e2+b.*4.14e2+c.*5.4e1+d.*6.0+3.12e2;a.*3.0e1+b.*5.4e1+c.*1.2e1+d.*6.0+3.0e1;a.*1.8e1+b.*6.0+c.*6.0+d.*6.0-2.4e1]
x =
[1] [2] [3] [4]
Fd =
7179
Gd =
1762
1608
228
48
Related
I indeed got a relatively big dataset and my mixed effects logistic regression is like below. Is that normal to take that long to run? or I made some mistakes?
library(lme4)
glmer_EBRD_undersample_1 <- glmer(leave_happened ~
performance_rating_2016 + performance_rating_2017 + performance_rating_2018 + performance_rating_2019 + performance_rating_2020
+ gender
+ target_group
+ target_pmf_band
+ target_hq_or_ro
+ target_office_location_country_distilled
+ target_org_unit_cost_centre_code_distilled
+ target_ebrd_region_distilled
+ target_contract_group_distilled
+ target_position_tenure_group
+ target_length_of_service_group_distilled
+ leaves_to_date
+ moves_to_date
+ joins_to_date
+ applied_count_to_date
+ line_reviewed_to_date
+ interviewed_to_date
+ offered_to_date
+ hired_to_date
+ (1 | person_id)
,
data = train_undersample_1,
family = binomial,
control = glmerControl(optimizer = "bobyqa"),
nAGQ = 10
)
summary(glmer_EBRD_undersample_1)
Also gave a warning like this: Warning in commonArgs(par, fn, control, environment()) :
maxfun < 10 * length(par)^2 is not recommended.
J1 = 18.041;
J2 = 1.99;
J3 = 6.58;
J4 = 1.46;
J5 = 7.50;
J6 = 15.46;
J7 = 4.21;
J8 = 7.68;
J9 = 24.84;
J10 = 8.58;
J11 = 6.57;
J12 = 4.20;
J13 = 4.63;
J14 = 0.7648;
J15 = 0.3607;
J16 = 68.48;
Js1 = 145.09;
Js2 = 128.58;
Js3 = 138.841;
Js4 = 145.09;
Jcar = 1.7916;
Jhollow = 1.478;
r1 = 45;
r2 = 46;
r3 = 38;
r4 = 15;
r5 = 37;
r6 = 68;
r7 = 29;
r8 = 37;
r9 = 60;
r10 = 33;
r11 = 29;
r12 = 27;
r13 = 52;
r14 = 19;
r15 = 16;
r16 = 57;
k0 = 241.32;
w2 = 1000;
kcar = 0.356;
khollow = 6.091;
D = 4867.91;
%syms h3(t) x3(t) theta2(t) theta1(t) x2(t) h4(t) h5(t) h1(t) h2(t) x1(t)
%ode1 = diff(h3)- (J4*r9*(diff(theta2,2)))/r4 == (theta2*khollow*r9)/r4 - kcar*theta1;
%ode2 = diff(x3,t) - (h3/J8)*r8 + w2*r3 == 0;
%ode3 = diff(x2,t) - (h2/J10)*r10 + w2*r5 == 0;
%ode4 = diff(theta2,t)*(r4- 1/(k0*r4)) - (diff(theta2,t,3)/(k0*r4)) - h3*r9/(J9 + Js1) + w2*r4 == 0;
%ode5 = diff(h1,t) - D*r16*(diff(x1)) == k0*x1*r16 - D*r16*((h4*r14)/(Js2 + J1 + (Jcar/3)));
%ode6 = diff(x1,t) - (h4)*r14/(Js2 + J1 + (Jcar/3)) + h1*r16/J16 == 0;
%ode7 = diff(h5,t) - x3*k0*r8 == 0;
%ode8 = diff(h2,t) - x2*k0*r10 == 0;
%ode9 = diff(h4,t) - kcar*theta1 - (2*h4*D)/(Js2 + J1 + (Jcar/3)) + k0*x1*r14 == 0;
%ode10 = diff(theta1,t) - h3/(J9 + Js1) + (h4)/(Js2 + J1 + (Jcar/3)) == 0;
%odes = [ode1; ode2; ode3; ode4; ode5; ode6; ode7; ode8; ode9; ode10];
%S = dsolve(odes);
f = #(t,x) [(J4*r9*(diff(x(4),2)))/r4 + (x(4)*khollow*r9)/r4 - kcar*x(10);
(x(1)*r8)/(J8) - w2*r3;
(x(8)/J10)*r10 - w2*r5;
(x(1)*r9)/(J9 + Js1)*r4 + (diff(x(4)) + diff(x(4),3))/(k0*r4*r4) - w2;
k0*x(6)*r16 - D*r16*(((x(9))*r14)/(Js2 + J1 + (Jcar/3)) - diff(x(6)));
(x(9)*r14)/(Js2 + J1 + (Jcar/3)) - (x(5)*r16)/J16;
x(2)*k0*r8;
x(3)*k0*r10;
kcar*x(10) - x(9)*2*D/(Js1 + J1 + (Jcar/3)) -k0*x(6)*r14;
x(1)/(J9 + Js1) - x(9)/(Js2 + J1 + (Jcar/3))];
[t,xa] = ode45(f,[0 1.5], [0 2 2 2 2 3 1 9 9 1])
Array dimensions are not consistent matlab output error. The commented out code runs for a long time and has not outputted results after running it for 15-20 minutes. The differentials equations are solved using ODE 45. Would really appreciate some help on this. The differential equations come from analyzing a dual clutch manual transmission using the bond graph technique.
Before putting a function like f into ode, try it. It is already broken and you get the same error when calling f(t,x). Further debugging it, evaluate the lines of f one by one:
>> (J4*r9*(diff(x(4),2)))/r4 + (x(4)*khollow*r9)/r4 - kcar*x(10)
ans =
[]
>> (x(1)*r8)/(J8) - w2*r3
ans =
-38000
The first row holds 0 elements, the second 1 element. I did not continue but this is already a problem. Each row has to have the same number of elements, probably 1 in your case.
I tried to print out a matrix using code as follow, however, it showed up nameerror.
i wonder where specifically should i define the matrix?
can python recognise the abbreviation as Qb to Q_bar?
import numpy as np
Q11 = 14.583
Q12 = 1.4583
Q23 = 0
Q22 = 3.646
Q33 = 4.2
theta = 60
def Q_bar(Q11, Q12, Q22, Q33, theta):
n = np.sin(theta*np.pi/180)
m = np.cos(theta*np.pi/180)
Qb_11 = Q11*m**4 + 2*(Q12 + 2*Q33)*n**2*m**2 + Q22*n**4
Qb_22 = Q11*n**4 + 2*(Q12 + 2*Q33)*n**2*m**2 + Q22*m**4
Qb_33 = (Q11 + Q22 - 2*Q12 - 2*Q33)*n**2*m**2 + Q33*(m**4 + n**4)
Qb_12 = (Q11 + Q22 - 4*Q33)*n**2*m**2 + Q12*(m**4 + n**4)
Qb_13 = (Q11 - Q12 - 2*Q33)*n*m**3 + (Q12 - Q22 + 2*Q33)*n**3*m
Qb_23 = (Q11 - Q12 - 2*Q33)*n**3*m + (Q12 - Q22 + 2*Q33)*n*m**3
Qb_matrix = np.array([[Qb_11, Qb_12, Qb_13],[Qb_12, Qb_22, Qb_23],[Qb_13, Qb_23, Qb_33]])
return(Qb_matrix)
print(Qb_matrix)
you never call your function so the code inside it is never executed. Further more even if you did call the function. The variable Qb_matrix which you create in the function will only exist inside the function scope, when you return it you need to store that returned value.
import numpy as np
Q11 = 14.583
Q12 = 1.4583
Q23 = 0
Q22 = 3.646
Q33 = 4.2
theta = 60
def Q_bar(Q11, Q12, Q22, Q33, theta):
n = np.sin(theta*np.pi/180)
m = np.cos(theta*np.pi/180)
Qb_11 = Q11*m**4 + 2*(Q12 + 2*Q33)*n**2*m**2 + Q22*n**4
Qb_22 = Q11*n**4 + 2*(Q12 + 2*Q33)*n**2*m**2 + Q22*m**4
Qb_33 = (Q11 + Q22 - 2*Q12 - 2*Q33)*n**2*m**2 + Q33*(m**4 + n**4)
Qb_12 = (Q11 + Q22 - 4*Q33)*n**2*m**2 + Q12*(m**4 + n**4)
Qb_13 = (Q11 - Q12 - 2*Q33)*n*m**3 + (Q12 - Q22 + 2*Q33)*n**3*m
Qb_23 = (Q11 - Q12 - 2*Q33)*n**3*m + (Q12 - Q22 + 2*Q33)*n*m**3
Qb_matrix = np.array([[Qb_11, Qb_12, Qb_13],[Qb_12, Qb_22, Qb_23],[Qb_13, Qb_23, Qb_33]])
return(Qb_matrix)
my_qb_matrix = Q_bar(Q11, Q12, Q22, Q33, theta)
print(my_qb_matrix)
OUTPUT
[[ 6.659175 1.179375 2.52896738]
[ 1.179375 12.127675 2.20689254]
[ 2.52896738 2.20689254 3.921075 ]]
I would like to create a loop for changing interactions name in PyMol. But after one selection loop it crashes and doesn't work.
def get_dists(interactions): # interactions=([1,2], [3,4])
for i in interactions:
a = "////" + str(i[0]) + "/C2'"
b = "////" + str(i[1]) + "/C2'"
cmd.distance("(" + a + ")", "(" + b + ")")
for j in range(1, 599):
x = "dist" + "0" + str(j)
y = str(i[0]) + " " + str(i[1])
cmd.set_name(str(x), str(y))
In Pymol the default name of interactions is dist01, 02 , 03.
I want to change these to 1_3, 5_59, 4_8, (interaction between residue).
Your code is totally fine except for one thing: If PyMol doesn't succeed with set_name the whole script is aborted. When you change it to, it should work:
try:
cmd.set_name(str(x), str(y))
except:
print('failed to rename')
Some additional comments:
y = str(i[0]) + " " + str(i[1]) should be y = str(i[0]) + "_" + str(i[1])
this line is probably for padding zeros x = "dist" + "0" + str(j). This is only needed when j is a single digit, otherwise the name of the distance objects is dist20 or dist123
cmd.set_name(str(x), str(y)) can be simplified to cmd.set_name(x, y) since x and y are already strings.
All is in the question, is there a way to display the € symbol in extjs?
I tried
var euroMoney = function(v) {
v = (Math.round((v - 0) * 100)) / 100;
v = (v == Math.floor(v)) ? v + ".00" : ((v * 10 == Math.floor(v * 10)) ? v + "0" : v);
v = String(v);
var ps = v.split('.'),
whole = ps[0],
sub = ps[1] ? ',' + ps[1] : ',00',
r = /(\d+)(\d{3})/;
while (r.test(whole)) {
whole = whole.replace(r, '$1' + '.' + '$2');
}
v = whole + sub;
return v + " €";
}
But for euroMoney(1) it returns me
1,00 €
This happens when your text editor saves it the wrong way. Make sure the encoding in your text editor is set to UTF-8 and you should be fine.