Differentiating symbolically then obtaining a numerical value - matlab

syms x
syms y
f = 2*x^2 + y
z = diff(f,x)
x = 3
SolvedDiffEq = z
Obviously z = 4x. But how do I solve to get the numerical answer of 12? I've played with vpa, double, subs and cannot figure it out.
This is a simple example. My actual code has very complex equations with many variables. Furthermore, I am trying to iteratively solve these equations so it is not practical to manually substitute each variable.

You are looking for symbolic substitution command, which is subs. In your example, subs(z) returns 12.

Related

Two MATLAB questions about syms

I use matlab syms to define a function
f(x,y,z) = ||y-zx||^2
where x and y are vectors of R^5, z is a scalar in R as follows
syms x y [5,1] matrix;
syms z;
f = (y-z*x).'*(y-z*x)
Now, I have two questions:
How to expand the expression of f using MATLAB?
How to define the function t-> d f(x+tw,y,z)/d t via an output of diff?
Thanks a lot for any comments and suggestions.
What I tried are summarized below:
How to expand the expression of f using MATLAB?
It seems that
expand(f)
does not work! and the other functions such as simplify, collect and factor work neither. So I want to know how to expand and simplify this expression in the correct way?
How to define the function t-> d f(x+tw,y,z)/d t via an output of diff?
I have tried to define the function phi(t) = f(x+tw,y,z) first as
syms x y w [5,1] matrix;
syms z t;
f = #(x,y,z) (y-z*x).'*(y-z*x);
phi = #(t) f(x+t*w,y,z);
Then I compute the derivative using diff:
diff(phi(t),t);
But I don't know how to make the resulting expression as a function of t (so that I can evaluate the expression of phi'(1))? For the moment, I just copy the expression of phi'(t) computed by diff and define it manually. Hoping to get a better way to do so. Thanks a lot for any comments and suggestions.

Solve a non-linear system of equations with differentiation in Matlab

I would like to solve a system of non-linear equations in Matlab with fsolve, but I also have to differentiate the functions with respect to two variables. Here is the problem in steps:
Step 1: I define the system of non-linear functions F:
function F = root2d(x)
F(1) = (exp(-x(1)+2)/(1+exp(-x(1)+2)+exp(-x(2)+1)));
F(2) = (exp(-x(2)+1)/(1+exp(-x(1)+2)+exp(-x(2)+1)));
end
Step 2: I want to find the derivative of F(1) with respect to x(1) and the derivative of F(2) with respect to x(2) such that:
function F = root2d(x)
F(1) = (exp(-x(1)+2)/(1+exp(-x(1)+2)+exp(-x(2)+1)));
F(2) = (exp(-x(2)+1)/(1+exp(-x(1)+2)+exp(-x(2)+1)));
f1_d=diff(F(1),x(1))
f1_d=diff(F(2),x(2))
end
Step 3: I want my function to be the original one plus the derivative:
function F = root2d(x)
F(1) = (exp(-x(1)+2)/(1+exp(-x(1)+2)+exp(-x(2)+1)));
F(2) = (exp(-x(2)+1)/(1+exp(-x(1)+2)+exp(-x(2)+1)));
f1_d=diff(F(1),x(1));
f1_d=diff(F(2),x(2));
F(1)=F(1)+f1_d;
F(2)=F(2)+f2_d;
end
Step 4: In the main file I would use this function with fsolve to solve the system of non-linear equations for x(1) and x(2):
syms x
fun = #root2d;
x0 = [0,0];
x = fsolve(fun,x0);
root2d(x)
function F = root2d(x)
F(1) = (exp(-x(1)+2)/(1+exp(-x(1)+2)+exp(-x(2)+1)));
F(2) = (exp(-x(2)+1)/(1+exp(-x(1)+2)+exp(-x(2)+1)));
f1_d=diff(F(1),x(1));
f1_d=diff(F(2),x(2));
F(1)=F(1)+f1_d;
F(2)=F(2)+f2_d;
end
Can you please help me, how can a rewrite my code in order to solve the derivatives and then calculate and solve the system? Calculation of the derivates by hand is not an option because later I have to develop this program and I will have about 100 equations instead of 2.
A fundamental problem that you're making is that you're solving this system numerically with fsolve but then mixing in symbolic math.
Since your functions, F don't seem to vary, their derivatives shouldn't either, so you can create them as symbolic functions and calculate their derivatives symbolically once:
syms x [1 2] real; % Create 1-by-2 symbolic vector
F = exp(-x+2)/(1+exp(-x(1)+2)+exp(-x(2)+1));
F_d(1) = diff(F(1),x(1));
F_d(2) = diff(F(2),x(2));
F = F+F_d; % Add derivatives to original functions
Then you can convert these to a numerical form and solve with fsolve:
root2d = matlabFunction(F,'Vars',{x}); % Convert symbolic function to vectorized numeric function
x0 = [0 0];
x = fsolve(root2d,x0)
root2d(x)
Note that fsolve only finds a single root (if it succeeds) for a given initial guess. It's strongly suggested that you plot your function to understand it's behavior and how many roots it may have. From my limited plotting it doesn't look like this function, as defined above, has any zeros. Separately, you should also look into the tolerance options for fsolve.
You can look into purely numerical (non-symbolic) methods to solve this, but you'll need properly calculate the derivative numerically. You might look into this approach for that.

Evaluating a complex integral in MATLAB

Hello I'm trying to integrate the following function in MATLAB
And this is my attempt at evaluating it at a given (x,y)
fun = #(t,x,y) exp(1i.*(t.^4+x.*t.^2+y.*t));
P = #(x,y) integral(#(t)fun(t,x,y),-Inf,Inf);
P(1,1)
According to WolframAlpha the answer is 1.20759 + 0.601534 i for P(1,1)
but MATLAB returns -6.459688464052636e+07 - 8.821747942103466e+07i
I am wondering how to enter an integral like this correctly.
I have now also tried evaluating this symbolically and using a taylor series to approximate but still no luck.
syms x y t
x=1
y=1
f = exp(1i*(t^4+x*t^2+y*t));
fApprox = taylor(f, t, 'ExpansionPoint', 0, 'Order', 10)
sol=int(fApprox,t,[-inf inf])
Any additional suggestions
Many thanks in advance.

Solving a complex double-integral equation for a third variable

I'm trying to solve the following equation for the variable h:
F is the normal cumulative distribution function
f is the probability density function of chi-square distribution.
I want to solve it numerically using Matlab.
First I tried to solve the problem with a simpler version of function F and f.
Then, I tried to solve the problem as below:
n = 3; % n0 in the equation
k = 2;
P = 0.99; % P* in the equation
F = #(x,y,h) normcdf(h./sqrt((n-1)*(1./x+1./y)));
g1 = #(y,h)integral(#(x) F(x,y,h).*chi2pdf(x,n),0,Inf, 'ArrayValued', true);
g2 = #(h) integral(#(y) (g1(y,h).^(k-1)).*chi2pdf(y,n),0,Inf)-P;
bsolve = fzero(g2,0)
I obtained an answer of 5.1496. The problem is that this answer seems wrong.
There is a paper which provides a table of h values obtained by solving the above equation. This paper was published in 1984, when the computer power was not good enough to solve the equation quickly. They solved it with various values:
n0 = 3:20, 30:10:50
k = 2:10
P* = 0.9, 0.95 and 0.99
They provide the value of h in each case.
Now I'm trying to solve this equation and get the value of h with different values of n0, k and P* (for example n0=25, k=12 and P*=0.975), where the paper does not provide the value of h.
The Matlab code above gives me different values of h than the paper.
For example:
n0=3, k=2 and P*=0.99: my code gives h = 5.1496, the paper gives h = 10.276.
n0=10, k=4 and P*=0.9: my code gives h = 2.7262, the paper gives h = 2.912.
The author says
The integrals were evaluated using 32 point Gauss-Laguerre numerical quadrature. This was accomplished by evaluating the inner integral at the appropriate 32 values of y which were available from the IBM subroutine QA32. The inner integral was also evaluated using 32 point Gauss-Laguerre numerical quadrature. Each of the 32 values of the inner integral was raised to the k-1 power and multiplied by the appropriate constant.
Matlab seems to use the same method of quadrature:
q = integral(fun,xmin,xmax) numerically integrates function fun from xmin to xmax using global adaptive quadrature and default error tolerances.
Does any one have any idea which results are correct? Either I have made some mistake(s) in the code, or the paper could be wrong - possibly because the author used only 32 values in estimating the integrals?
This does work, but the chi-squared distribution has (n-1) degrees of freedom, not n as in the code posted. If that's fixed then the Rinott's constant values agree with literature. Or at least, the literature that isn't behind a paywall. Can't speak to the 1984 Wilcox.

fzero with a matrix and solution of previous fzero as an input

This is an evolution of the question asked by Immo here
We have a function f(x,y) and we are trying to find the value of x (x*) for a function f(x,y) that produces the function value 0 for a given Y, where Y is a large vector of values.
x* = arrayfun(#(i) fzero(#(x) minme(y(i),x),1),1:numel(y))
What if now I would like to use the x* solution of the arrayfun to find another fzero of the second equation, lets say minme2, that also depends on the same large vector of values Y.
minme2 = #(y,x*, x) x - Y + x* + x* ./ (const1 * (const2 - x*))
Moreover I would like my solutions to dynamically depend on the interval I am selecting for the fzero solution, rather than the initial guess. Instead of 1 I would like my solutions to be found in between:
x0 = [0, min(const1, x*)];
I would appreciate answers how can I solve the minme2 as currently I am facing errors.