How to solve for a series of variables that intersect with each other?

조회 수: 2 (최근 30일)
Chao Wang
Chao Wang 2019년 2월 23일
댓글: Walter Roberson 2019년 2월 24일
Hi folks, recently I got a problem about solving a series of variables which have the property like:
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10, where x1 and x10 are known, and the relationships are as following:
x2=x1^2+x3^3;
x3=x2^2+x4^2;
x4=x3^2+x5^2;
...
Intuitively there are in total 8 unkown variables and 8 equations so we can get value of each of them, but how can I solve these functions in matlab? What if there are more than just 10 variables, say 100, from x1 to x100, how can I solve the functions for x2 to x99? I tried to solve this as solving a system of linear equation but failed.
  댓글 수: 5
Chao Wang
Chao Wang 2019년 2월 23일
편집: Chao Wang 2019년 2월 23일
I tried as what you said, and this is what I wrote:
M=95;
r=0.02;
q=0;
dt=0.02;
sigma=0.18;
a=zeros(M-1,1);
b=zeros(M-1,1);
c=zeros(M-1,1);
a(1)=M-1;
b(1)=M-1;
c(1)=M-1;
for j=2:M-1
a(j)=a(j-1)-1;
b(j)=b(j-1)-1;
c(j)=c(j-1)-1;
end
for j=1:M-1
a(j)=(r-q)*(M+1-j)*dt/2-sigma^2*(M+1-j)^2*dt/2;
b(j)=1+sigma^2*(M+1-j)^2*dt+r*dt;
c(j)=-(r-q)*(M+1-j)*dt/2-sigma^2*(M+1-j)^2*dt/2;
end
parameter=zeros(M-1,M+1);
for j=1:M-1
parameter(j,j)=c(j);
parameter(j,j+1)=b(j);
parameter(j,j+2)=a(j);
end
b=zeros(94,1);
myfunc=@(x) parameter*[0;x;12]==b;
x=solve(myfunc,x);
but how to claim the dimension of x? If I don't do anything, the system just thinks [0;x;12] is a 3*1 vector rather than 94*1 vector.
Walter Roberson
Walter Roberson 2019년 2월 24일
x = sym('x', [94, 1]);
myfunc = parameter*[0;x;12]==b;
sol = solve(myfunc, x);
X = vpa( struct2cell(sol) );

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by