Equation with multiple variables: Finding all combinations x1,x2 that lead to certain outcome y.

3 次查看(过去 30 天)
I have a formula with two variables: x1 and x2
x1 and x2 can both vary in certain regimes: x1 varying in between
[x1min x1max]
x2 varying in between
[x2min x2max]
Now I have the formula:
y = a * x1.^b * x2.^c
in which a,b and c are known constants.
Now I would like to find and store all combinations of x1 and x2 that lead to a certain outcome of y+-10%.
So, lets say: y = 30 +-3.0 with x1 = between [0.3 0.4] and x2 = between [0.1 1].
n = 100;
x1 = linspace(0.3, 0.4, n); x2 = linspace(0.1,1, n)
30 = 5 *x1.^-1.3 * x2.^-1.95;
Now I can write an extensive code in the way of trial and error: A code that calculates every combination and then filtering which solutions fall outside of y+-10%.
But I was wondering whether there is a more clever way: A way for Matlab to actually solve this problem, or reduce computational time? As I need to do this for several outcomes of y...

回答(1 个)

Navya Seelam
Navya Seelam 2020-2-19
编辑:Navya Seelam 2020-2-19
You can use meshgrid to obtain all possible combinations of x1 and x2 followed by feval to evaluate the function as shown below
syms x1 x2
y = matlabFunction(5 *x1.^-1.3 * x2.^-1.95) ;
n = 100;
x1a = linspace(0.3, 0.4, n); x2a = linspace(0.1,1, n);
[X1 X2]=meshgrid(x1a,x2a);
p=feval(y,X1,X2);
ind=find(abs(p-30)<3); % indices of data points which satisfy the condition y+-10%.
x1req=X1(ind);
x2req=X2(ind);

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by