MATLAB
Introduction
In this example, we will explore how to submit a MATLAB program to Multivac to compute a simple objective function.
Running MATLAB from the command line
Run:
/usr/local/programari/matlab2024b/bin/matlab -nodisplay -nojvm
Please note that startup may take a little while.
Example Code
Suppose we want to minimize the following objective function:
We will create a file named test_matlab.m to compute the objective function value for a specific value of x:
% Define the objective function
f = @(x) x^2 + 3*x + 2;
% Define a specific value of x
x_val = 5;
% Compute the objective function value for x_val
result = f(x_val);
% Print the result
fprintf('The objective function value for x = %d is %f\n', x_val, result);
This code defines the objective function, assigns a specific value to x (in this case, 5), and computes the objective function result for that value of x.
Execution
We will create a configuration file, for example test_matlab.slurm, containing the execution settings for this MATLAB script.
VERSION=1.3
JOB_NAME=matlab_example
NAME_OUTPUT=out
PARTITION=all
N_TASKS=1
CPUS_PER_TASK=1
MAIL_TYPE=END,FAIL
MAIL_USER=nom.usuari@upc.edu
MEMORY=1G
BEGIN=now
TIME_LIMIT=00:05:00
LOG_OUTPUT=log
FORCED_NODES=
EXCLUDED_NODES=
ROUTE=~/
COMMANDS=(
"/usr/local/programari/matlab2024b/bin/matlab -nodisplay -r \"run('$ROUTE/test_matlab.m'); exit;\" "
)
We will submit this script from iocex using the following command:
multivac test_matlab.slurm
Once execution is complete, the output of the program will be visible in the same directory where it was launched.