Simple Solver-type function

조회 수: 11 (최근 30일)
Julia
Julia 2011년 5월 16일
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.

채택된 답변

Matt Tearle
Matt Tearle 2011년 5월 16일
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
Julia
Julia 2011년 5월 16일
My God, it's rediculously simple. Thank you very much for your help.
Matt Tearle
Matt Tearle 2011년 5월 16일
Wait until you see what happens when I actually think for a moment. See the above edit...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by