Does MATLAB have a feature to show step-by-step solution?

12 views (last 30 days)
Janice
Janice on 28 Jun 2022
Commented: Janice on 28 Jun 2022
Hello everyone.
I have a pretty complicated equation where I need to find the solution for B. I substituted the values of variables and separated the equation into 4 big "parts" to make it easier to read.
100-B==
100*exp(-(0.05)*m*(0.15))
*normcdf(-((log(B)+(0.01)*m*(0.15)-log(100)+((0.05)-(0.03)-(0.2)^2/2)*m*(0.15))/((0.2)*sqrt(m*(0.15)))))
-exp(-((0.03)-(0.01))*m*(0.15))*B
*normcdf(-((log(B)+(0.01)*m*(0.15)-log(100)+((0.05)-(0.03)+(0.2)^2/2)*m*(0.15))/((0.2)*sqrt(m*(0.15)))))
+(0.15)*(0.05)*100*symsum(exp(-(0.05)*(m-j)*(0.15)) ...
*normcdf(-((log(B)+(0.01)*m*(0.15)-(log(B)+(0.01)*(m-j)*(0.15))+((0.05)-(0.03)-(0.2)^2/2)*(m-j)*(0.15)) ...
/((0.2)*sqrt((m-j)*(0.15))))),j,0,m-1)
-(0.15)*((0.03)-(0.01))*exp((0.01)*m*(0.15))*B*symsum(exp(-((0.03)+(0.01))*(m-j)*(0.15))* ...
normcdf(-((log(B)+(0.01)*m*(0.15)-(log(B)+(0.01)*(m-j)*(0.15))+((0.05)-(0.03)+(0.2)^2/2)*(m-j)*(0.15)) ...
/((0.2)*sqrt((m-j)*(0.15))))),j,0,m-1)
where m denotes time and B denotes "price". I used vpasolve(<equation>,B) to solve the equation.
When I used m=4, it returns a real value for the solution, but starting from m=5, an imaginary component showed up. B is not supposed to have imaginary values at any point of time m.
I tried decomposing the equation and evaluating the value at each "part" at m=5 to figure out where the imaginary component originated from, but I cannot seem to find anything that results in a negative value inside the square root. Does MATLAB have a feature to show step-by-step solution?
Thank you very much!
  6 Comments

Sign in to comment.

Answers (1)

Torsten
Torsten on 28 Jun 2022
m = 5;
B0 = 1;
B = fsolve(@(B)fun(B,m),B0)
B = 1
B = 1.0000
B = 2
B = 2.0000
B = 4.5000
B = 4.5000
B = 10.7500
B = 10.7500
B = 26.3750
B = 26.3750
B = 65.4375
B = 65.4375
B = 126.9775
B = 80.8225
B = 80.8225
B = 81.8887
B = 81.8887
B = 81.8346
B = 81.8346
B = 81.8344
B = 81.8344
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
B = 81.8344
fun(B,m)
B = 81.8344
ans = 1.9267e-10
function res = fun(B,m)
B
sum1 = 0.0;
sum2 = 0.0;
for j=0:m-1
sum1 = sum1 + exp(-(0.05)*(m-j)*(0.15)) ...
*normcdf(-((log(B)+(0.01)*m*(0.15)-(log(B)+(0.01)*(m-j)*(0.15))+((0.05)-(0.03)-(0.2)^2/2)*(m-j)*(0.15)) ...
/((0.2)*sqrt((m-j)*(0.15)))));
sum2 = sum2 + exp(-((0.03)+(0.01))*(m-j)*(0.15))* ...
normcdf(-((log(B)+(0.01)*m*(0.15)-(log(B)+(0.01)*(m-j)*(0.15))+((0.05)-(0.03)+(0.2)^2/2)*(m-j)*(0.15)) ...
/((0.2)*sqrt((m-j)*(0.15)))));
end
res = 100*exp(-(0.05)*m*(0.15))...
*normcdf(-((log(B)+(0.01)*m*(0.15)-log(100)+((0.05)-(0.03)-(0.2)^2/2)*m*(0.15))/((0.2)*sqrt(m*(0.15)))))...
-exp(-((0.03)-(0.01))*m*(0.15))*B...
*normcdf(-((log(B)+(0.01)*m*(0.15)-log(100)+((0.05)-(0.03)+(0.2)^2/2)*m*(0.15))/((0.2)*sqrt(m*(0.15)))))...
+(0.15)*(0.05)*100*sum1 ...
-(0.15)*((0.03)-(0.01))*exp((0.01)*m*(0.15))*B*sum2 - (100-B);
end
  4 Comments
Janice
Janice on 28 Jun 2022
I just ran the code and it worked perfectly.
Thank you very much for all your help!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by