Matlab Solve System of Equations with Quantized Variables

拟墨画扇 提交于 2019-12-04 18:38:18

I'm not sure what you're trying to do here. Can you please post a real example (with numbers) and what you would like the output to be?


I think you're trying to solve linear simultaeneous equations. Assuming that is what you are trying to do:

I would suggest multiplying all of your equations by 20, so that your minimum quanta size of 0.05 becomes 1.00. Your problem then becomes the solution of linear equations for integer values.

Note that if the system is fully constrained (that is, if there are n independent constraints on the n equations you want to solve) then there will only be one solution and it may not necessarily be an integer solution. For example the system:

1 = 2a + 4b
3 =  a +  b

has the solution a = 5.5, b = -2.5. No other solution is possible.

For under-constrained systems, i.e.

0 = 3x + y
x > 0

Then there will be an infinite number of solutions, some of which may have both x and y being integer values. (Or there may be no integer solutions at all.)

Okay let me give you a quick rundown.

if you want to solve an equation or a system of equations and conditions then you need to define them as such, so let me explain. so by example

clear all; %just to be safe
syms x y b 
a=0.5;
somevalue=1;
someothervalue=3;
eq1= a*x+a*y == somevalue; %this is your first equation
eq2= b*x+b*y == someothervalue; %this is your 2nd equation
cond1= x<5; %this is a condition which matlab sees as an "equation"
eqs=[eq1,eq2,cond1]; %these are the equations and conditions you want to solve for, use this for solve
eqs=[eq1,eq2]; %use this for vpasolve and set your condition in range
vars=[x,y,b]; %these are the variable you want to solve for
range = [-Inf 5; NaN NaN; NaN NaN]; %NaN means you set no range

%you can use solve or vpasolve, second one being numeric, which is the one you'll probably want
n=5;
sol=zeros(n,numel(vars));
for i = 1:n 
    temp1 = vpasolve(eqs, vars, range, 'random', true);
    temp = vpasolve(eqs, vars, 'random', true);
    sol(i,1) = temp.x;
    sol(i,2) = temp.y;
    sol(i,3) = temp.b;
end
sol

Now when I run this myself I can't get the range to properly work for some reason, still trying to figure that out. When you don't set a range it works just fine, if you can use the solve function then there also isn't a problem. In theory the range function should work fine like this so it might be a bug on my end.

If you use solve you have some nice options where you can use assume to set extra conditions that are a bit more advanced, like only checking for real solutions or only integers, etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!