MATLAB: In an assignment A(:) = B, the number of elements in A and B must be the same.

for loop

My code is:
pvals = 10:1:15;
nump = length(pvals);
for pidx = 1 : nump
p=pvals(pidx);
[x(pidx),y(pidx)]=solve('9*x(pidx)+8*y(pidx)=p','13*x(pidx)+14*y(pidx)=p+2','x(pidx)','y(pidx)');
end
plot(pvals,x)
The error is:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Any help is appreciated! Thanks!

Best Answer

My version of the Symbolic Math Toolbox doesn’t like quoted strings, so I had to change your code a bit.
Try this:
syms p x y
pvals = 10:1:15;
nump = length(pvals);
for pidx = 1 : nump
p=pvals(pidx);
[xs(pidx),ys(pidx)]=solve([9*x+8*y==p, 13*x+14*y==p+2], [x y]);
end
plot(pvals,xs)