How to convert symbolic expressions into numeric?

4 views (last 30 days)
Max on 29 Jan 2016
Edited: Stephen23 on 30 Jan 2016
Hello,
I´ve wrote the following code by using symbolic variables. But I´ve got a problem the memory.
Can somebody help me to rewrite it so that I have only numeric variables?
tfail = [5571.760,5573.742,5654.457,6079.693,6081.927,6172.915,6515.064,6517.515,6617.308,7095.558,7098.298,7209.831,7530.929,7533.885,7654.224,7966.300,7969.472,8098.617,8401.671,8405.059,8543.009,8982.166,8985.843,9135.533,9852.908,9857.017,10024.38,10868.774,10873.387,11061.234];
n=length(tfail);
beta_hat = 5.2;
B_hat = 1200;
C_hat = 60;
syms t B beta C
y(t) = (exp(-B/((heaviside(t)-heaviside(t-2000))*(330)+(heaviside(t-2000)-heaviside(t-3000))*(350)+...
(heaviside(t-3000)-heaviside(t-14000))*(390))))/C;
logL=0;
for i=1:n
tfail(i);
I(i) = int(y(t),t,0,tfail(i));
y_new(i)=subs(y,t,tfail(i));
logL =logL+log((beta*y_new(i)*(I(i))^(beta-1))*exp(-((I(i))^beta)));
end
p = int(y(t),t,0,14000);
u = beta*log(p);
du_dB = diff(u,B);
du_dbeta = diff(u,beta);
du_dC = diff(u,C);
du_dB_sub = subs(du_dB,{beta,B,C},{beta_hat,B_hat,C_hat});
du_dbeta_sub = subs(du_dbeta,{B,C},{B_hat,C_hat});
du_dC_sub = subs(du_dC,{beta,B,C},{beta_hat,B_hat,C_hat});
v=[beta;B;C];
H=hessian(logL,v);
H_negatv=-1*H;
h = 1\H_negatv
w=subs(h,[beta,B,C],[beta_hat,B_hat,C_hat]);
F_direct = w;
Var_beta_hat_direct = double(F_direct(1,1));
Var_B_hat_direct = double(F_direct(2,2));
Var_C_hat_direct = double(F_direct(3,3));
Cov__B_C_direct = double(F_direct(2,3));
Cov__beta_C_direct = double(F_direct(1,3));
Cov__beta_B_direct = double(F_direct(1,2));

Walter Roberson on 29 Jan 2016
In
I(i) = int(y(t),t,0,tfail(i));
you invoke y as a function, fitting in with the way you defined y(t) = ....
But in the next line,
y_new(i)=subs(y,t,tfail(i));
you substitute into the function y, creating a new function, rather than invoking y as a function.
Then on the line after that,
logL =logL+log((beta*y_new(i)*(I(i))^(beta-1))*exp(-((I(i))^beta)));
you are recalling this new function and multiplying it by things and so on, so you are doing calculations on functions instead of on expressions.
Walter Roberson on 30 Jan 2016
It would help if you gave more information about the problem you are encountering. Are you running out of memory? Your H is only about 5 1/2 megabytes.
Your H is 3 x 3, so your H_negative is 3 x 3. When you take 1\H_negative then that corresponds to inv(1)*H_negative which is going to be exactly the same as H_negative. Where you looking for inv(H_negative) ? That would be eye(3)/H_negative and Yes that could occupy rather a lot of memory and time to compute symbolically. However, you are not planning to loop on the response substituting numerous different values into the symbols, so you should substitute the numeric values in first, double() that to get a numeric array, and then take the inverse. Fast and little memory.