Minimizing expected value using a multivariate normal distribution Matlab - matlab

I am trying to minimise the expected value of this function w.r.t the variable Q :
where f(D) is the probability density function of a multivariate normal distribution, c0 and cu parameters.
This is what I have done so far :
syms X1
syms X2
syms X3
c0 = 1 ;
cu = 1 ;
x = [X1; X2; X3] ;
mu = [3; 5; 7 ] ;
sigma = [4,3,6;3,8,5;6,5,10] ;
s = mvnpdf(x,mu,sigma)
syms Q
syms D
f = (Q-D).*s ;
z = c0.*int(f,D,0,Q) ;
anon_z = matlabFunction(z) ;
g = (D-Q).*s ;
t = cu.*int(g,D,Q,inf) ;
anon_t = matlabFunction(t) ;
tot = #(Q) anon_z(Q) + anon_t(Q) ;
[x fx] = fminsearch(tot,0)
It gives me this error :
Error in
symengine>#(Q,X1,X2,X3)Q.^2.*exp(conj(X1).conj(X3).(3.0./2.0)).*exp(conj(X2).conj(X3).(1.0./1.1e1)).exp(conj(X1).-3.0).exp(conj(X2).(3.0./1.1e1)).exp(conj(X3).(2.6e1./1.1e1)).exp(conj(X1).^2.(-5.0./4.0)).exp(conj(X2).^2.(-1.0./1.1e1)).exp(conj(X3).^2.(-2.3e1./4.4e1)).exp(-8.75688228083863).(1.0./2.0)
Error in #(Q)anon_z(Q)+anon_t(Q)
Error in fminsearch (line 189) fv(:,1) = funfcn(x,varargin{:});
Error in multi_3giocatori (line 19) [x fx] = fminsearch(tot,0)
Can you help me fix this?

Related

Find roots using fsolve

Basically I would like to use the fsolve command in order to find the roots of an equation.
I think I should create a function handle that evaluates this equation in the form "right hand side - left hand side =0", but I've been struggling to make this work. Does anyone know how to do this?
The equation itself is 1/sqrt(f) = -1.74log((1.254/((1.27310^8)sqrt(f)))+((110^-3)/3.708)). So I would like to find the point of intersection of the left and right side by solving for 1/sqrt(f)+(1.74log((1.254/((1.27310^8)sqrt(f)))+((110^-3)/3.708))) = 0 using fsolve.
Thanks a lot!
The code so far (not working at all)
f = #(x) friction(x,rho,mu,e,D,Q, tol, maxIter) ;
xguess = [0, 1];
sol = fsolve(x, xguess ) ;
function y = friction(x,rho,mu,e,D,Q, tol, maxIter)
D = 0.1;
L = 100
rho = 1000;
mu = 0.001;
e = 0.0001;
Q = 0.01;
U = (4*Q)/(pi*D^2);
Re = (rho*U*D)/mu ;
y = (1/sqrt(x))-(-1.74*log((1.254/(Re*sqrt(x)))+((e/D)/3.708)))
end
Error message:
Error using lsqfcnchk (line 80)
FUN must be a function, a valid character vector expression, or an inline function object.
Error in fsolve (line 238)
funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag);
Error in Untitled (line 6)
sol = fsolve(x, xguess ) ;
opt = optimset('Display', 'Iter');
sol = fsolve(#(x) friction(x), 1, opt);
function y = friction(x)
D = 0.1;
L = 100; % note -- unused
rho = 1000;
mu = 0.001;
e = 0.0001;
Q = 0.01;
U = (4*Q)/(pi*D^2);
Re = (rho*U*D)/mu ;
y = (1/sqrt(x))-(-1.74*log((1.254/(Re*sqrt(x)))+((e/D)/3.708)));
end
sol = 0.0054
the first argument of fsolve should be the function not variable. Replace:
sol = fsolve(x, xguess );
with
sol = fsolve(f, xguess );
And define Rho, mu, e etc before you define f (not inside the friction function).

Matlab error while calculating double integral

I'm trying to calculate a double integral but get an error.
My code is:
clear
Gamma = rand([22,1]);
data_i = rand([1,10]);
Y_1 = 1;
fun = #(D_star, Y_0 ) fun1(D_star , Y_1 , Y_0 , Gamma , data_i);
p = integral2 ( fun , 0 , 1 , 0 , 1);
and the function fun1 is defined as
function [p] = fun1(D_star , Y_1 , Y_0 , Gamma , data_i)
gamma=Gamma(1:3);
beta_1=Gamma(4:5);
beta_0=Gamma(6:7);
alpha_D=Gamma(8);
alpha_1=Gamma(9);
alpha_0=Gamma(10);
sigma2_1=Gamma(11);
sigma2_0=Gamma(12);
Lambda=Gamma(13:17);
sigma2_M=Gamma(18:22);
Sigma2_temp = [1 ; sigma2_1 ; sigma2_0 ; sigma2_M];
Alpha = [alpha_D ; alpha_1 ; alpha_0 ; Lambda];
Sigma2 = Alpha * Alpha' + diag(Sigma2_temp);
Z = data_i(3:5);
X = data_i(3:4);
M = data_i(6:10);
Mu = [Z*gamma , X*beta_1 , X*beta_0 , zeros(1,5) ]';
YY = [D_star ; Y_1 ; Y_0 ; M' ];
p = mvnpdf(YY,Mu,Sigma2);
end
I don't think there is a problem in the definition of the functions because I can evaluate them (e.g. fun(1,1) give me an answer). The error message that I get is:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in fun1 (line 23)
YY = [D_star ; Y_1 ; Y_0 ; M' ];
Error in #(D_star,Y_0)fun1(D_star,Y_1,Y_0,Gamma,data(n,:))
Error in integral2Calc>integral2t/tensor (line 228)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
I tried also the command -quad2d- instead of -integral2- and got a similar error. Any ideas?
I found the problem. According to the documentation, when using -integral2-, the function inside the integral must get an array of both variables as an input and return an array as an output.

Solving nonlinear system of differential equations in matlab usin ODE45

i am trying to solve a system of nonlinear differential equation using ODE45 MATLAB , i did that many times successfully , but this time i get the following error and i really don't know what is wrong i am confused compeletly. here are the codes.
%% this is the error:
Subscript indices must either be real positive integers or logicals.
Error in non_L_ss (line 6)
(-Fk*(ds0+x(3)-x(1))+Fk*ds0-Fc(x(4)-x(2)))/ms +Fa/ms ] ;
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed,solver_name,ode, tspan,y0,optio varargin);
Error in solve (line 50)
[t X]=ode45(#non_L_ss,t_span,IC);
%% the equations be defined in a function:
function dX=non_L_ss(t,x)
global Fk Fc kt Fa q ds0 ms mu
dX=[ x(2);
(Fk*(ds0+x(3)-x(1))-Fk*ds0+Fc*(x(4)-x(2))-kt*x(1))/mu-Fa/m-kt*q/mu ;
x(4);
(-Fk*(ds0+x(3)-x(1))+Fk*ds0-Fc(x(4)-x(2)))/ms +Fa/ms ] ;
end
%% and here the function be called to solve by ODE45:
clear
clc
global Fk Fc kt Fa q ds0 ms mu qdot v2
mu = 100 ;
ms = 1242 ;
k1s = 80000 ;
k2s = 32000 ;
kt = 405000 ;
c1s = 4000 ;
c2s = 1600 ;
v = 20 ;
Gq = 256e-6 ;
ds0 = 0.1538 ;
a = 1 ;
b = 0.001 ;
n0 = 0.1 ;
f0 = 0.011*v ;
w = 0.5 ;
Fa = 2000 ;
q = 0.05 ;
xs = 0.1 ;
xu = 0.1 ;
dxs = 0.1 ;
dxu = 0.2 ;
Fk = k1s+k2s*(ds0+xs-xu).^2 ;
if dxs >= dxu
Fc = c1s ;
elseif dxs < dxu
Fc = c2s ;
end
t_span=[0 1];
IC=[2 3 2 2];
[t X]=ode45(#non_L_ss,t_span,IC);
Subscript indices must either be real positive integers or logicals.
Error in non_L_ss (line 6)
(-Fk*(ds0+x(3)-x(1))+Fk*ds0-Fc(x(4)-x(2)))/ms +Fa/ms ] ;
means that you use something as an indexed array with a non-integer index. Which probably means that the object is no array at all. I'd propose to replace
Fc(x(4)-x(2))
by
Fc*(x(4)-x(2))
to try to solve this issue.

How to plot a vector array having symbolic variables?

**I want to plot an array vector having symbolic variables in it. But I am getting an error which is given below.
??? Error using ==> plot
Conversion to double from sym is not possible.
Error in ==> dsolvenew at 36
plot(t2,qn2) **
The code is given below.
syms A Epsilon0 k1 k2 e s d R s f T c0 v0 v
An = 0.5*10^(-9);
Epsilon0n = 8.85*10^-12;
k1n = 10;
k2n = 10;
en = 1.6*10^-19;
sn = 1.8*10^(-9);%m gap size%
dn = 3*sn; % total distance
Rn = 1;
c0n=(An*Epsilon0n*k1n)/dn;
t1=0:0.1:5;
fn=1000;
v0n=7.5;
Tn=5/f;
vn=v0n*sin(2*3.14*fn*t1);
plot(t1,vn);
inits = 'q(0)=(20*10^(-20)),Q(0)=0';
[q,Q] = dsolve('Dq=((v/R)-(1/R)*(((d*q)+(s*Q))/(c0*d)))','DQ= (((q+Q)/(A*Epsilon0*k1))*s)',inits);
qn1=vpa(subs(q,{A,Epsilon0,k1,k2,e,s,d,R,c0}, {An,Epsilon0n,k1n,k2n,en,sn,dn,Rn,c0n}),3);
i=1;
while i<length(t1);
qn2(i)=subs(qn1, v, vn(i));
i=i+1;
end
t2=t1(1:50);
plot(t2,qn2)

variable in solving the equation

I want to solve equations in matlab, eg.
100+a/2=173*cos(b)
sqrt(3)*a/2=173*sin(b)
and the code would be:
[a,b]=solve('100+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')
However, if I want to take 100 as a variable, like
for k=1:100
[a,b]=solve('k+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')
end
There would be an error, how to make it?
degree=140/1000000;
p=42164000;
a=6378136.5;
b=6356751.8;
x_1=0;
y_1=p;
z_1=0;
for i=451:550
for j=451:550
alpha=(1145-i)*degree;
beta=(1145-j)*degree;
x_2=p/cos(alpha)*tan(beta);
y_2=0;
z_2=p*tan(alpha);
syms x y z x_1 x_2 y_1 y_2 z_1 z_2 a b
eq = [(x-x_1)*(y2-y_1)-(x_2-x_1)*(y-y_1),(x-x_1)*(z_2-z_1)-(x_2-x_1)*(z-z_1), b^2*(x^2+y^2)+a^2*(y^2)-a^2*b^2 ];
sol = solve(eq(1),x,eq(2),y, eq(3),z);
sol.x
sol.y
sol.z
end
end
I got the expression value, how do I get the numeric value of x,y,z?
[['x(1)=';'x(2)='],num2str(double(sol.x))]
not work ,shows
??? Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in ==> sym.sym>sym.double at 927
Xstr = mupadmex('mllib::double', S.s, 0);
Error in ==> f2 at 38
[['x(1)=';'x(2)='],num2str(double(sol.x))]
If you have access to the Symbolic Toolkit then you do the following:
syms a b k
eq = [k+a/2-173*cos(b), sqrt(3)*a/2-173*sin(b)];
sol = solve(eq(1),a,eq(2),b);
sol.a = simplify(sol.a);
sol.b = simplify(sol.b);
% There are two solutions for 'a' and 'b'
% check residuals for example k=20
subs(subs(eq,{a,b},{sol.a(1),sol.b(1)}),k,20)
% ans = 0.2e-13
subs(subs(eq,{a,b},{sol.a(2),sol.b(2)}),k,20)
% ans = 0.2e-13
Edit 1
Based on new code by OP the matlab script to solve this is:
clear all
clc
syms alpha beta
degree=140/1000000;
p=42164000;
a=6378136.5;
b=6356751.8;
x_1=0;
y_1=p;
z_1=0;
x_2 = p/cos(alpha)*tan(beta);
y_2 = 0;
z_2 = p*tan(alpha);
syms x y z
eq = [(x-x_1)*(y_2-y_1)-(x_2-x_1)*(y-y_1);...
(x-x_1)*(z_2-z_1)-(x_2-x_1)*(z-z_1); ...
b^2*(x^2+y^2)+a^2*(y^2)-a^2*b^2 ];
sol = solve(eq(1),x,eq(2),y,eq(3),z);
sol.x = simplify(sol.x);
sol.y = simplify(sol.y);
sol.z = simplify(sol.z);
pt_1 = [sol.x(1);sol.y(1);sol.z(1)] % First Solution Point
pt_2 = [sol.x(2);sol.y(2);sol.z(2)] % Second Solution Point
x = zeros(100,100);
y = zeros(100,100);
z = zeros(100,100);
for i=451:550
disp(['i=',num2str(i)])
for j=451:550
res = double(subs(pt_1,{alpha,beta},{(1145-i)*degree,(1145-j)*degree}));
x(i-450, j-450) = res(1);
y(i-450, j-450) = res(2);
z(i-450, j-450) = res(3);
end
end
disp('x=');
disp(x);
disp('y=');
disp(x);
disp('z=');
disp(x);
I would try
for i=1:100
k=num2str(i)
[a,b]=solve('100+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')
end
and then solve the equation