How do I differentiate and evaluate in Matlab? - matlab

I need to differentiate exp((s^2*sigma^2)/2 + mu*s) and evaluate it to s=0.
Anyone can provide advice on the syntax or how I should go about performing this differentiation?
If it helps, the above function is the mgf of a standard normal.
I want to differentiate and evaluate s=0 so I can get the mean,variance,skew and kurtosis.
Thanks!

Try this :
>> syms s sigma mu
>> f=diff(exp((s^2*sigma^2)/2 + mu*s),s)
f =
exp((s^2*sigma^2)/2 + mu*s)*(s*sigma^2 + mu)
>> subs(f, 's', 0)
ans =
mu
>> subs(f, 's', 1)
ans =
exp(sigma^2/2 + mu)*(sigma^2 + mu)
Ref - diff, subs

Related

How to find the unique solution to norm(a,2)==c; when c is a scalar and a=[x abs(1/x);x+1 1/x]

I am trying to find the unique solution to the following equation using Matlab
norm(a,2)=0.11
when x is a variable and a=[x abs(1/x);x+1 1/x]. b is the exact formulation of norm(a,2), which I have gained:
syms x
a=[x abs(1/x);x+1 1/x];
b = norm(a,2)
b = max(abs(2*x + x*abs(x)^2 + 2*x*abs(x)^4 + abs(x)^4 + x^2*abs(x)^2 - (4*x*abs(x)^5 + 2*x*abs(x)^6 + 4*x*abs(x)^8 + abs(x)^8 + 8*x^2*abs(x)^3 + x^2*abs(x)^4 + 4*x^3*abs(x)^3 + 2*x^3*abs(x)^4 + 6*x^2*abs(x)^6 + x^4*abs(x)^4 + 4*x^3*abs(x)^6 + 4*x^2*abs(x)^8 + 4*x^2)^(1/2))/(2*abs(x)^3), abs(2*x + x*abs(x)^2 + 2*x*abs(x)^4 + abs(x)^4 + x^2*abs(x)^2 + (4*x*abs(x)^5 + 2*x*abs(x)^6 + 4*x*abs(x)^8 + abs(x)^8 + 8*x^2*abs(x)^3 + x^2*abs(x)^4 + 4*x^3*abs(x)^3 + 2*x^3*abs(x)^4 + 6*x^2*abs(x)^6 + x^4*abs(x)^4 + 4*x^3*abs(x)^6 + 4*x^2*abs(x)^8 + 4*x^2)^(1/2))/(2*abs(x)^3))^(1/2)
I have tried solve() and the following is the result:
solve(b==0.11,x)
Warning: Cannot find explicit solution.
ans =
Empty sym: 0-by-1
Any help regarding the suitable method to solve the above equation will be appreciated.
As far as I know
norm(a) = max(svd(a))
max is a problem in this equation, that's why I would find and solve symbolic equations for the both svd results separately:
syms x
a=[x abs(1/x);x+1 1/x];
s = svd(a);
% svd 1
solve(s(1)==0.11,x)
% svd 2
solve(s(2)==0.11,x)
Return value for svd 1:
Warning: Cannot solve symbolically. Returning a numeric approximation instead.
ans = -12.84595601211006224344551434882
Return value for svd 2:
Warning: Cannot find explicit solution.
ans = Empty sym: 0-by-1
So the answer would be
ans = -12.84595601211006224344551434882
If we had a solution for each svd part, we could find max() of them.
Here is a plot of both svd functions:
UPDATE
As we could see above, the solver switched to the numerical solver and found only one solution, although the plot shows us at least two possible solutions.
To find all solution (there can be actually more then two) I would directly use the numerical solver vpasolve and either input an initial guess or let the solver found solutions randomly:
vpasolve(s(1)==0.11,x,2) % input initial guess as 2
It returns the second solution:
ans = 2.2626424161863046178372248086765
Or use the random guess:
for n = 1:10
vpasolve(s(1)==0.11,x,'Random',true) % use random guess
end
It returns all found solution:
ans = -12.84595601211006224344551434882
ans = -12.84595601211006224344551434882
ans = -12.84595601211006224344551434882
ans = -12.84595601211006224344551434882
ans = 2.2626424161863046178372248086765 %!!!
ans = -12.84595601211006224344551434882
ans = -12.84595601211006224344551434882
ans = -12.84595601211006224344551434882
ans = -12.84595601211006224344551434882
ans = -12.84595601211006224344551434882

fminsearch error: DOUBLE cannot convert the input expression into a double array

I am encountering a problem during an optimization exercise. I am trying to use fminsearch() in matlab to solve it. The following error is generated when the code is run:
The following error occurred converting from sym to double: Error
using symengine (line 59) DOUBLE cannot convert the input expression
into a double array. If the input expression contains a symbolic
variable, use VPA.
Error in fminsearch (line 190) fv(:,1) = funfcn(x,varargin{:});
Error in Optimization (line 22) sol2 = fminsearch(J, x0);
The script I use can be seen below. The f is the minimization problem where g1 & g2 are constraints. The p is there so that I can turn it into for loop later.
syms x1 x2 x3 x4;
syms p;
f = x1.^4 - x3.^2 + x1*x3 + x2*x4 - 5*x2 + 3;
g1 = x1.^2 + x2.^2 + x3.^2 + x4.^2 - 1;
g2 = x1 + x3 - 1;
x0 = [2 2 2 2];
p = 3;
J = #(x1,x2,x3,x4) sym(f + p * g1.^2 + g2.^2);
sol2 = fminsearch(J, x0);
This Stackoverflowpost has the same problem but in another perspective.According to this post it might be a problem with allocating the in a valid way. I tried a few different ways to solve my problem. I have tried matlabFunction() and putting the function in a seperate file.
If the input expression contains a symbolic variable, use the VPA function instead?
Thanks in advance for the help.
fminsearch is designed for numerical minimization. There is little point in using symbolic math in this case (it can be used, but it will be slower, complicate your code, and the results will still be in double precison). Second, if you read the documentation and look at the examples for fminsearch, you'll see that it requires a function that takes a single vector input (as opposed to four scalars in your case). Here's how you could rewrite your equations using anonymous functions:
f = #(x)x(1).^4 - x(3).^2 + x(1).*x(3) + x(2).*x(4) - 5*x(2) + 3;
g1 = #(x)x(1).^2 + x(2).^2 + x(3).^2 + x(4).^2 - 1;
g2 = #(x)x(1) + x(3) - 1;
x0 = [2 2 2 2];
p = 3;
J = #(x)f(x) + p*g1(x).^2 + g2(x).^2;
sol2 = fminsearch(J, x0)
this returns
sol2 =
0.149070165097281 1.101372214292880 0.326920462283209 -0.231885482601008
Using symbolic math and subs:
syms x1 x2 x3 x4;
f = x1^4 - x3^2 + x1*x3 + x2*x4 - 5*x2 + 3;
g1 = x1^2 + x2^2 + x3^2 + x4^2 - 1;
g2 = x1 + x3 - 1;
x0 = [2 2 2 2];
p = sym(3);
J = #(X)subs(f + p*g1^2 + g2^2,[x1 x2 x3 x4],X);
sol2 = fminsearch(J, x0)
which returns identical results.

How can I solve a system of 4 equations and 4 unknowns with MATLAB?

I have a general equation
t=tr+(ts-tr)/(1+(a*h)^n)^(1-1/n)
for (h=0, 1, 2, 3), I have t=2.000, 1.6300, 1.2311, 1.1084. therefor there are 4 equations with 4 unknowns tr, ts, a, n
I used "solve" function in matlab
s=solve('tr+(ts-tr)/(1+(a*0)^n)^(1-1/n)=2','tr+(ts-tr)/(1+(a*1)^n)^(1-1/n)=1.63','tr+(ts-tr)/(1+(a*2)^n)^(1-1/n)=1.2311','tr+(ts-tr)/(1+(a*3)^n)^(1-1/n)=1.1084')
and error is
??? Error using ==> mupadmex
Error in MuPAD command: Singularity [ln];
during evaluation of 'numeric::fsolve'
Error in ==> sym.sym>sym.mupadmexnout at 2018
out = mupadmex(fcn,args{:});
Error in ==> solve at 76
[symvars,R] = mupadmexnout('symobj::solvefull',eqns,vars);
What should I do?
The problem appears with you using the solve function. That only works for simple equations, it is better to use the fsolve function. Due to the fact that I am worried that I am doing an assignment for you, I am only going to show you how to do another example using fsolve.
Suppose that you want to solve
1 = x_1
1 = x_1 + x_2
-1 = x_1 + x_2 + x_3
-1 = x_1 + x_2 + x_3 + x_4
then what you firstly need to do is write these as equations equal 0
0 = x_1 - 1
0 = x_1 + x_2 - 1
0 = x_1 + x_2 + x_3 + 1
0 = x_1 + x_2 + x_3 + x_4 + 1
then you need to write a function that takes in a vector x, the components of x will represent x_1, x_2, x_3 and x_4. The output of the function will also be a vector whose components should the outputs of the Right hand side of the above equations (see the function fun below). This function is going to be called by fSolve for it to provide it with guesses of the correct value of x, until it guess correct. When never actually run this function ourselves. That is why it is below the top function.
Then you create a function handle to this function by fHandle = #fun. You can think of fHandle as another name for fun, when we calculate fHandle([1; 2; 3; 4]) this is the same as calculating fun([1; 2; 3; 4]). After this you make an initial guess of the correct vector x, say we chose xGuess = [1; 1; 1; 1]. Finally we pass fHandle and xGuess to fSolve.
Here is the code
function Solve4Eq4Unknown()
fHandle = #fun;
xGuess = ones(4,1);
xSolution = fsolve(fHandle, xGuess)
end
function y = fun(x)
y = zeros(4,1); % This step is not necessary, but it is effecient
y(1) = x(1) - 1;
y(2) = x(1) + x(2) - 1;
y(3) = x(1) + x(2) + x(3) + 1;
y(4) = x(1) + x(2) + x(3) + x(4) + 1;
end

How to plot this equation in matlab

alright well I have the follow function
y=sin(x)^2 + [(10+2x+x^2) / (5+2x^2)]
i need to plot it on the interval y = -2 to y = 2 so how would I set that up?
I did this in matlab
>> y = sin(x).^2 + (10 + 2*x + x.^2)/(5+2*x.^2)
>> x = -2:0.01:2;
is that a correct setup? Or have I done something wrong
You need to declare a variable before you use it. In this case, x doesn't depend on y, so declare it first. In addition, there is a ./ operator missing.
x = -2:0.01:2;
y=sin(x).^2 + (10+2*x+x.^2) ./ (5+2*x.^2);
plot(x,y)
f = #(x) sin(x)^2 + [(10+2*x+x^2) / (5+2*x^2)];
ezplot(f)

Matlab: Polynomial Expansion Routine

In Mathematica, it's easy to expand terms like
(ax^2+bx+c)^n
But is there anyway I can do this in Matlab?
For any arbitrary expression: not without the Symbolic Toolbox.
http://www.mathworks.com/help/toolbox/symbolic/expand.html
However, if you wish to expand polynomials, you can use the conv function. Just run it in a loop.
a = 1;
b = 2;
c = 3;
n = 5;
soln = [a b c];
for i=1:n-1
soln = conv(soln,[a b c]);
end
You can also use my sympoly toolbox.
>> sympoly a b c x
>> (a*x^2+b*x+c)^3
ans =
c^3 + 3*b*c^2*x + 3*b^2*c*x^2 + b^3*x^3 + 3*a*c^2*x^2 + 6*a*b*c*x^3 + 3*a*b^2*x^4 + 3*a^2*c*x^4 + 3*a^2*b*x^5 + a^3*x^6