How to plot a function with a vector as output
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Good day everybody!
I use MATLAB to create a neural network with the "fitnet" function to "fit" a function.
My problem is the following:
As input for the network, I create a vector with the input parameters [7x1], as a target for the training I specify a vector with the target parameters [3x1].
After training the network, I get a function "net1" which gives me an output vector [3x1] for an input vector [7x1].
So far so good.
I would now like to use the function to:
1. Vary two of the input variables (contained in the input vector) and to plot an output variable (contained in the first output vector element).
2. Create a maximum value search for the elements in the output vector by vary the elements in the input vector.
The problem is that by outputting the function as a vector, most of the MATLAB commands will not work.
I have already tried to limit the output to the first element using the "table" and "subsref" commands, which works in the command bar. In commands like "surf" or "fminsearch", however, leads to "Error using vertcat; Dimensions of arrays being concatenated are not consistent.".
I hope someone can help me with my problem and thank you in advance!
Marvin
0 comentarios
Respuestas (1)
Naren
el 8 de Abr. de 2024
Hello Marvin,
I understand you are trying to create neural network with “fitnet” function and would like to visualize and optimize your neural network’s output.
1. Plotting an Output Variable by Varying Two Input Variables:
To plot an output variable (the first element of the output vector) by varying two of the input variables, you can use a meshgrid to generate a grid of input values and then apply your neural network to each pair of input values. Assuming the two variables you want to vary are the first and second elements of the input vector, you can do something like this:
x1_range = linspace(minValueX1, maxValueX1, 100); % Replace minValueX1 and maxValueX1 with your actual range
x2_range = linspace(minValueX2, maxValueX2, 100); % Replace minValueX2 and maxValueX2 with your actual range
[X1, X2] = meshgrid(x1_range, x2_range);
% Initialize the output matrix
output = zeros(size(X1));
% Other inputs remain constant, define them
otherInputs = [constantValue3; constantValue4; constantValue5; constantValue6; constantValue7]; % Replace constantValueN with actual values
for i = 1:numel(X1)
inputVec = [X1(i); X2(i); otherInputs];
outputVec = net1(inputVec);
output(i) = outputVec(1);
end
% Plotting
surf(X1, X2, output);
xlabel('Input 1');
ylabel('Input 2');
zlabel('Output 1');
title('Output 1 as a function of Input 1 and Input 2');
2. Maximum Value Search in the Output Vector
For searching the maximum value in the output vector by varying elements in the input vector, you can use optimization functions such as fmincon (since fminsearch does not directly support multi-dimensional outputs). You will need to define an objective function that returns the negative of the output of interest (since fmincon minimizes rather than maximizes). Here's an example:
% Objective function
objectiveFunc = @(inputVec) -net1(inputVec)(1); % Modify this line if needed to correctly access the first output
% Initial guess for the input vector
initialGuess = [0; 0; 0; 0; 0; 0; 0]; % Replace with a reasonable initial guess
% Define bounds for the inputs if known (optional)
lb = [-Inf; -Inf; -Inf; -Inf; -Inf; -Inf; -Inf]; % Lower bounds
ub = [Inf; Inf; Inf; Inf; Inf; Inf; Inf]; % Upper bounds
% Use fmincon to find the maximum
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
[maxInput, negMaxOutput] = fmincon(objectiveFunc, initialGuess, [], [], [], [], lb, ub, [], options);
% The maximum output is the negative of negMaxOutput because we minimized its negative
maxOutput = -negMaxOutput;
Note: You might need to adjust the objective function definition to ensure it correctly accesses the first element of the output vector. The way to access elements might differ based on how your net1 function outputs data.
I hope this helps.
Regards,
Naren
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!