Solving simultaneous equation on each iteration

1 view (last 30 days)
Hi there, I'm trying to find the value of two functions q and p on each loop of the following code, but unfortunately the right results aren't coming out.
clear;
clc;
strategy1=2;
strategy2=2;
maxscore=2;
m=maxscore;
n=maxscore;
k=maxscore;
p=sym(zeros(maxscore+1,maxscore+1,maxscore+1));
q=sym(zeros(maxscore+1,maxscore+1,maxscore+1));
p(m,n,k)=1;
q(m,n,k)=1;
for k=maxscore:-1:1
if k<=strategy1+1 & m+k<=maxscore+1
syms x
q(n,m,1)=x;
p(m,n,k)= 0.5*((1-q(n,m,1))+p(m,n,k+1));
pp=p(m,n,k);
end
if k>=strategy1+1 & m+k<=maxscore+1
p(m,n,k)= 1-q(n,m+k-1,1);
end
if k<=strategy2+1 & n+k<=maxscore+1
q(n,m,k)= 0.5*((1-p(m,n,1)+q(n,m,k+1)));
qp=q(n,m,k);
true_x=solve(qp,x);
end
if k<=strategy1+1 & m+k<=maxscore+1
p(m,n,k)= 0.5*((1-true_x)+p(m,n,k+1));
end
if k>=strategy2+1 & n+k<=maxscore+1
q(n,m,k)= 1-p(m,n+k-1,1);
end
end
Basically the value of true_x is coming out as -2 on the first iteration and it should be 2/3 as q(2,2,1)=x=0.5((1-(p(2,2,1))+1) =0.5((1-(1-0.5x)+1)
  2 Comments
Star Strider
Star Strider on 27 Mar 2019
When I tried to run your posted code:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right
side is 0-by-1.
p(m,n,k)= 0.5*((1-true_x)+p(m,n,k+1));
In that iteration, m = n = 1 and k = 2.
Samson Leach
Samson Leach on 27 Mar 2019
ah that's my bad, I have edited it now so that the first two loops are gone so the code only attempts to find the first points p(2,2,1) and q(2,2,1), you will see that true_x comes out as -2 instead of 2/3

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements 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!