0

I got a function of three parameters that I wanted to plot in matlab and I varied the values of the parameters (50 values for each parameter) for creating a multidimensional array 50x50x50. At first I want to plot it with surf, then with contourn3, but I don't know how to do this. I want to see the trend of this function.

1

1 Answer 1

1

If I understand right, you have some sort of function like value = fun(parmA, parmB, parmC), and want to visualize how value changes w.r.t. the parameters. Unfortunately there is no way to visualize all of the data at once in a single graphic. You have to choose one of the dimensions to hold constant.

We can visualize 3-D data using a surface plot, surf(), or level contours contour(). These work when we have something like value = fun(parmA, parmB), or z = fun(x,y). So the best alternative you have is step through one of the dimensions and generate a new surface or contour plot for each value.

Using a short delay, you can actually generate a video. Here is a simple example. You might have to do a little extra work if you want to hold the axes constant throughout.

clear all; close all; clc

% create your parameters
x = linspace(-50,50,51);
y = linspace(-100,100,52);
z = linspace(0,50,50);

% A function of three variables
fun = @(x,y,z) sqrt(x^2 + y^2 + z^2);

% Preallocate
vaue = zeros(50,50,50);

% Populate the data matrix
for i = 1:numel(x)
    for j = 1:numel(y)
        for k = 1:numel(z)
            value(i,j,k) = fun(x(i),y(j),z(k));
        end
    end
end

% Generate a new surface or contour plot for each value of "z"
for k = 1:numel(z)
    figure(1)
    %contour(x,y,value(:,:,k)');
    surf(x,y,value(:,:,k)');
    title(sprintf('z = %f',z(k)));
    pause(0.1);
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.