MATLAB Problem Set 1: Finding Max Values and Evaluating Functions

often find ourselves faced with intriguing challenges and fascinating computations. Today, we'll embark on two journeys—one involving a 4x5 matrix and its maximum value, and the other into the realm of mathematical functions. So, fasten your seatbelts, and let's dive in!

Finding the Maximum Value in a Matrix

Imagine you're presented with a 4x5 matrix, an array of numbers arranged in rows and columns. Your task? To unearth the maximum value within this matrix and pinpoint its exact location. Here's a snippet of MATLAB code that accomplishes this feat:

% Create a 4x5 matrix (you can replace this with your own data)
matrix = [
    10  5   8   12  7;
    3   17  6   39   11;
    2   14  20  4   1;
    15  16  13  18  19
];

% Find the maximum value in the matrix
maxValue = max(matrix, [], 'all');

% Find the row and column indices of the maximum value
[row, col] = find(matrix == maxValue);

% Display the result
fprintf('Maximum value: %d\n', maxValue);
fprintf('Position of maximum value: Row %d, Column %d\n', row, col);

This code creates a matrix, locates the maximum value and its position, and delivers the exciting results. You can easily adapt it for your own matrices, exploring the peaks of your data landscapes.

If you want to randomize the matrix, try replacing the first line of code with the below:

matrix = randi([0,100],4,5);

Also, another way of finding the maximum value in the matrix can be:

maxValue = max(matrix(:));

matrix(:) is an operation that reshapes the matrix into a single-column vector by stacking all the columns on top of each other. In other words, it "unrolls" the matrix into a one-dimensional array.


Evaluating Mathematical Functions

Mathematical functions are like the hidden treasures of MATLAB, waiting to be unlocked with a few lines of code. Consider the function f(x, y) = x^2 + sin(xy) + 2y. With MATLAB, you can quickly evaluate this function for any given values of x and y. Here's how:

% Define the function
function f = fun(x, y)
    f = x^2 + sin(x * y) + 2 * y;
end

% Evaluate the function with specific values
result = fun(2, 3);

% Display the result
fprintf('Result of the function: %.2f\n', result);

This code defines the function fun(x, y), evaluates it with x = 2 and y = 3, and unveils the outcome. You can modify the values of x and y to explore the function's behavior across different domains.

With these MATLAB adventures, you've scratched the surface of what this versatile tool can do. From matrix manipulations to mathematical evaluations, MATLAB opens doors to a world of mathematical exploration and problem-solving. So, whether you're crunching numbers, analyzing data, or unraveling mathematical mysteries, MATLAB is your trusty companion on this exhilarating journey. Stay tuned for more MATLAB marvels in the world of computation and discovery! 🌟🔢