Simple Solver-type function

5 views (last 30 days)
Julia
Julia on 16 May 2011
I am new to MATLAB and was wondering if someone could help me with a simple function. The idea is as follows:
I have two equations:
r_u = r0 + theta*delta + sigma*sqrt(delta)
r_d = r0 + theta*delta - sigma*sqrt(delta)
I need to find theta such that the above equations are satisfied as well as the equation below:
P = exp(-r0*delta)[0.5*exp(-r_u*delta) + 0.5*exp(-r_d*delta)]*100.
r0, delta, sigma and P are known. I need the function to calculate theta, r_u and r_d such that the above equations are true. I can do it in Excel using Solver but MATLAB is new to me and I would really appreciate any help I can get.

Accepted Answer

Matt Tearle
Matt Tearle on 16 May 2011
If you have Optimization Toolbox, you can use fsolve. Rewrite the three equations in the form F(x) = 0, where x is a vector of your three unknowns.
% set values
r0 = rand;
delta = rand;
sigma = rand;
P = rand;
% make function
f = @(x) [r0 + x(3)*delta + sigma*sqrt(delta) - x(1);
r0 + x(3)*delta - sigma*sqrt(delta) - x(2);
exp(-r0*delta)*(0.5*exp(-x(1)*delta) + 0.5*exp(-x(2)*delta))*100 - P];
% initial guess
x0 = rand(3,1);
% find solution
x0 = fsolve(f,x0)
Here, x(1) is r_u, x(2) is r_d, x(3) is theta.
EDIT TO ADD Having looked more closely at the math, I realized that you don't even need fsolve. The problem can be reduced to a single equation in one variable (theta):
% set values
r0 = rand;
delta = rand;
sigma = rand;
P = rand;
% make functions
r_u = @(theta) r0 + theta*delta + sigma*sqrt(delta);
r_d = @(theta) r0 + theta*delta - sigma*sqrt(delta);
f = @(theta) exp(-r0*delta)*(0.5*exp(-r_u(theta)*delta) + 0.5*exp(-r_u(theta)*delta))*100 - P;
% initial guess
theta0 = rand;
% find solution
theta0 = fzero(f,theta0)
r_u(theta0)
r_d(theta0)
  2 Comments
Julia
Julia on 16 May 2011
My God, it's rediculously simple. Thank you very much for your help.
Matt Tearle
Matt Tearle on 16 May 2011
Wait until you see what happens when I actually think for a moment. See the above edit...

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!