Solve a system of equations having variables as a arrays
2 views (last 30 days)
Show older comments
Abdulmanan Butt
on 30 Nov 2018
Answered: Star Strider
on 30 Nov 2018
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.
0 Comments
Accepted Answer
Star Strider
on 30 Nov 2018
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.
0 Comments
More Answers (1)
madhan ravi
on 30 Nov 2018
Edited: madhan ravi
on 30 Nov 2018
syms x y %where x is x(1) and y is x(2) change it throughout the code you will get the result
after solving for x and y define variable arrays and use subs(x) and subs(y) to substitute all the points
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!