Does MATLAB have a feature to show step-by-step solution?
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
If vpasolve iterates B such that it becomes negative, log(B) will become complex-valued.
Restrict B to be positive:
assume(B,'positive')
Janice
on 28 Jun 2022
Christine Tobler
on 28 Jun 2022
When you specify the symbolic variable B, you can use
syms B real
instead of just
syms B
That should prevent returning complex values - although there's a chance that it would just return no solution if it otherwise finds only complex solutions.
By the way, what is j here? Is it another constant defined before this line is run, or does it denote sqrt(-1)?
Janice
on 28 Jun 2022
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.
Janice
on 28 Jun 2022
Answers (1)
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
Janice
on 28 Jun 2022
Image Analyst
on 28 Jun 2022
Was that function in its own m-file called fun.m? Or was it part of another script or m-file? Sounds like the latter. If you have a script and functions in the same m-file, the functions must all end with an "end" statement and come AFTER the script.
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
Janice
on 28 Jun 2022
Categories
Find more on Symbolic Math Toolbox 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!