Subs Error while using solve function
이전 댓글 표시
Hello!
I have question about using solve function.
I'm trying to derive yield to maturity using forward contract prices.
During the process, I tried to use solve function as shown below, which gives me error about
not able to transform data into double and recommending usage of 'subs' function.
I think i am doing this in the worst way possible, so Could you please give me the solution about this error?
Thanks.
T = rows(Data_full);
yield = zeros(T,1);
syms R P
for t = 1:T
eqn = (5/2)/(1+(R/2))+(5/2)/(1+(R/2)^2)+(5/2)/(1+(R/2)^3)+(5/2)/(1+(R/2)^4)+(5/2)/(1+(R/2)^5)...
+(5/2)/(1+(R/2)^6)+100/(1+(R/2)^6) == P;
P = Data_full(t,1);
yield(t) = solve(eqn,R);
end
yield
답변 (2개)
Dyuman Joshi
2023년 12월 26일
Allocate the value to P before defining the equation solve().
T = rows(Data_full);
yield = zeros(T,1);
syms R P
for t = 1:T
%Define the value for P
P = Data_full(t,1);
%Equation to solve for
eqn = (5/2)/(1+(R/2))+(5/2)/(1+(R/2)^2)+(5/2)/(1+(R/2)^3)+(5/2)/(1+(R/2)^4)+(5/2)/(1+(R/2)^5)...
+(5/2)/(1+(R/2)^6)+100/(1+(R/2)^6) == P;
%Solution
yield(t) = solve(eqn,R);
end
yield
Or you can directly use the value, no need to define P -
T = rows(Data_full);
yield = zeros(T,1);
syms R
for t = 1:T
%Equation to solve for
eqn = (5/2)/(1+(R/2))+(5/2)/(1+(R/2)^2)+(5/2)/(1+(R/2)^3)+(5/2)/(1+(R/2)^4)+(5/2)/(1+(R/2)^5)...
+(5/2)/(1+(R/2)^6)+100/(1+(R/2)^6) == Data_full(t,1);
%Solution
yield(t) = solve(eqn,R);
end
yield
댓글 수: 1
Dyuman Joshi
2023년 12월 26일
You will have to find criteria to sort out the solution you want. E.g. for real solutions only you could use
syms R
P = 10;
eqn = (5/2)/(1+(R/2))+(5/2)/(1+(R/2)^2)+(5/2)/(1+(R/2)^3)+(5/2)/(1+(R/2)^4)+(5/2)/(1+(R/2)^5)...
+(5/2)/(1+(R/2)^6)+100/(1+(R/2)^6) == P;
SOL = double(solve(eqn,R))
SOL = SOL(abs(imag(SOL))<1e-5)
카테고리
도움말 센터 및 File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!