evaluate two variable symbolic expression for every combination of values between 10 and -10 in matlab

One way to evaluate a two variable symbolic expression for every combination of values between 10 and -10 in MATLAB is to use a for-loop and the subs function to substitute each combination of values into the symbolic expression. Here is an example code that demonstrates this approach:

main.m
syms x y f(x,y) % Define symbolic variables and expression

f(x,y) = x^2 + y^2; % Example expression

x_values = linspace(-10, 10, 21); % Generate 21 values between -10 and 10
y_values = linspace(-10, 10, 21);

results = zeros(length(x_values), length(y_values)); % Initialize results matrix

for i = 1:length(x_values)
    for j = 1:length(y_values)
        x_val = x_values(i);
        y_val = y_values(j);
        results(i,j) = subs(f, [x,y], [x_val,y_val]); % Substitute values and evaluate
    end
end

% Plot results
[X,Y] = meshgrid(x_values, y_values);
surf(X,Y,results);
581 chars
21 lines

In this example, the linspace function is used to generate 21 equally spaced values between -10 and 10 for both x and y. The results matrix is initialized to store the evaluated expression for each combination of x and y values. The for-loops iterate over all combinations of x and y values and subs is used to substitute each combination into the symbolic expression f and evaluate it. Finally, the results are plotted using the surf function.

gistlibby LogSnag