Borrar filtros
Borrar filtros

Regression Learner model with multiple variables: Finding all combinations x1,x2 that lead to certain outcome y

3 visualizaciones (últimos 30 días)
I have created a model using the Regression Learner toolbox that takes three input variables x1, x2 & x3 and accurately predicts an outcome y. Is there a way to then calculate all possible combinations of x1 and x2 that lead to a certain value for y, given x3?
I am fairly new to MATLAB and don't know where to even start with this.
Thanks!

Respuesta aceptada

Tushar
Tushar el 21 de Mzo. de 2023
Hi,
Yes, there is a way to calculate all possible combinations of x1 and x2 that lead to a certain value for y, given x3.
Here's a MATLAB code example that demonstrates how to do this:
% Load the regression model
load my_regression_model.mat;
% Specify the desired value for y and x3
y_desired = 5;
x3 = 2;
% Define a range of values for x1 and x2
x1_range = linspace(0, 10, 100); % adjust range and number of points as needed
x2_range = linspace(0, 10, 100);
% Create a meshgrid of all possible combinations of x1 and x2
[x1_grid, x2_grid] = meshgrid(x1_range, x2_range);
% Reshape the grid into a vector for prediction
x1_vec = reshape(x1_grid, [], 1);
x2_vec = reshape(x2_grid, [], 1);
% Create a matrix of the input variables
X = [x1_vec, x2_vec, repmat(x3, numel(x1_vec), 1)];
% Use the regression model to predict y for each combination of x1 and x2
y_pred = predict(my_regression_model, X);
% Find the indices of the combinations that lead to the desired value of y
idx = find(abs(y_pred - y_desired) < 0.01);
% Extract the corresponding values of x1 and x2
x1_desired = x1_vec(idx);
x2_desired = x2_vec(idx);
In this code example, we first load the regression model (my_regression_model) that was created using the Regression Learner toolbox. We then specify the desired value for y (y_desired) and the value of x3 (x3).
Next, we define a range of values for x1 and x2 (x1_range and x2_range) and use meshgrid to create a grid of all possible combinations of x1 and x2. We reshape the grid into a vector for prediction and create a matrix of the input variables (X).
We then use the predict function to predict y for each combination of x1 and x2. We find the indices of the combinations that lead to the desired value of y (idx) and extract the corresponding values of x1 and x2 (x1_desired and x2_desired).
Note that in the find function, we use a tolerance of 0.01 to account for small differences between the predicted values and the desired value of y. You may need to adjust this tolerance depending on the precision of your regression model and the desired level of accuracy.
Hope it helps!!

Más respuestas (0)

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by