How to solve equations with possible values for variables?
1 view (last 30 days)
Show older comments
I'm working in a simulation project, and there is a step that I'd like to solve using MATLAB. I'll explain a very simplified system, but still similar to the one in the project. It goes as following: I have a set of 3 variables, and 4 conditions:
a1*4 + a2*3 + a3*1 = 6
a1 can be only 1 or 0
a2 can be only 1 or 0
a3 can be only 1 or 0
So, based on those restrictions, the only possible solution would be a1 = 1, a2 = 0, a3 = 1. How can I write this in matlab, and get this answer?
Thanks!
0 Comments
Answers (1)
Joshua
on 27 Mar 2017
This is not a very elegant solution, but it works.
clear
clc
syms a1 a2 a3
f=symfun(4*a1+3*a2+2*a3,[a1,a2,a3])
for i=0:1
for j=0:1
for k=0:1
if f(i,j,k)==6
answers=[i,j,k]
break;
end
end
end
end
if isempty(answers)==true
error('No solution found')
end
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!