Does MATLAB have a feature to show step-by-step solution?
48 views (last 30 days)
Show older comments
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
Christine Tobler
on 28 Jun 2022
If it's already defined as a symbol, j won't have any effect like that. I was just wondering as an outside possibility - if you had forgotten to define j, it might have been that its default value of sqrt(-1) was used instead.
Answers (1)
Torsten
on 28 Jun 2022
m = 5;
B0 = 1;
B = fsolve(@(B)fun(B,m),B0)
fun(B,m)
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
Torsten
on 28 Jun 2022
Edited: Torsten
on 28 Jun 2022
Load all this in the editor and run it.
By "run" I mean click on the green RUN arrow to the right at the task bar.
m = 1:100;
B0 = 1;
B = arrayfun(@(m)fsolve(@(B)fun(B,m),B0),m)
%fun(B,m)
plot(m,B)
function res = fun(B,m)
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
See Also
Categories
Find more on Assumptions 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!