MATLAB Answers

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

1 view (last 30 days)
Willemijn Wolf
Willemijn Wolf on 29 Jan 2020
Edited: Navya Seelam on 19 Feb 2020
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...

  0 Comments

Sign in to comment.

Answers (1)

Navya Seelam
Navya Seelam on 19 Feb 2020
Edited: Navya Seelam on 19 Feb 2020
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);

  0 Comments

Sign in to comment.


Translated by