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

55 次查看(过去 30 天)
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 个评论
Christine Tobler
Christine Tobler 2022-6-28
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.

请先登录,再进行评论。

回答(1 个)

Torsten
Torsten 2022-6-28
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 个评论
Torsten
Torsten 2022-6-28
编辑:Torsten 2022-6-28
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
Janice 2022-6-28
I just ran the code and it worked perfectly.
Thank you very much for all your help!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by