Matlab Solve System of Equations with Quantized Va

2020-07-27 18:38发布

问题:

I am trying to use solve() to solve a system of equations of the following form

eq1=a1x+a2y;
eq2=b1x+b2y;

where a1 = .05 for values of x<5, .1 for values of 5

Is there a way to solve for this using solve? As in sol = solve(eq1,eq2);

回答1:

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.)



回答2:

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.