MATLAB: Solve a system of equations having variables as a arrays

solve

I am trying this code in MATLAB, but getting errors
clear all
close all
clc
syms x(i)
eq1= x(1) + 3*x(2) == 7
eq2= x(1) + x(2) == 2
eq=[eq1,eq2]
v=[x(1),x(2)]
solve(eq,v)
Why i cant solve these system of equations using solve? Kindly guide me it will be of immense help.

Best Answer

You need to define ‘x’ as a vector uisng the sym function.
Then it works:
syms x
x = sym('x',[1 2]); % <— ADD THIS ASSIGNMENT
eq1= x(1) + 3*x(2) == 7;
eq2= x(1) + x(2) == 2;
eq=[eq1,eq2];
v=[x(1),x(2)];
vs = solve(eq,v);
X = [vs.x1, vs.x2]
producing:
X =
[ -1/2, 5/2]
No other changes in your code are necessary.