Statements and macros¶
Statements describe algorithmic actions that can be executed. There are two different types of control statements, conditional and repetitive. The first group defines conditional jumps whereas the latter one defines repetition until a conditional statement is fulfilled. Macros are used to define new functions to the MATLAB or CALFEM structure, or to store a sequence of statements in an .m-file.
Control statements¶
|
Conditional jump |
|
Initiate a loop |
|
Define a conditional loop |
Macros and Functions¶
|
Define a new function |
|
Store a sequence of statements |
if¶
- Purpose:
Conditional jump.
- Syntax:
if condition
statements
elseif condition
statements
else
statements
end
- Description:
The
ifstatement allows conditional execution of code blocks. Theconditionis evaluated and if true (nonzero), the correspondingstatementsare executed.- Examples:
Simple if statement:
if x > 0 y = sqrt(x); else y = 0; endMultiple conditions:
if x > 0 result = 'positive'; elseif x < 0 result = 'negative'; else result = 'zero'; end- Note:
In MATLAB, any nonzero value is considered true, and zero is false.
for¶
- Purpose:
Initiate a loop.
- Syntax:
for variable = expression
statements
end
- Description:
The
forloop repeatsstatementsfor each value in theexpression. Thevariabletakes on each value from the expression in sequence.- Examples:
Simple counting loop:
for i = 1:10 fprintf('Iteration %d\n', i); endLoop over array elements:
A = [1, 4, 9, 16]; for element = A disp(element); endNested loops:
for i = 1:3 for j = 1:3 A(i,j) = i + j; end end- Note:
The loop variable takes on the values column by column if the expression is a matrix.
while¶
- Purpose:
Define a conditional loop.
- Syntax:
while condition
statements
end
- Description:
The
whileloop repeatsstatementsas long as theconditionremains true (nonzero).- Examples:
Simple while loop:
i = 1; while i <= 10 fprintf('Count: %d\n', i); i = i + 1; endConvergence loop:
error = 1; tolerance = 1e-6; while error > tolerance % ... computation ... error = abs(new_value - old_value); end- Note:
Be careful to ensure the condition eventually becomes false to avoid infinite loops.
function¶
- Purpose:
Define a new function.
- Syntax:
function [output1, output2, ...] = function_name(input1, input2, ...)
statements
end
- Description:
The
functionkeyword defines a new function with specified inputs and outputs. Functions should be saved in .m files with the same name as the function.- Examples:
Simple function:
function y = square(x) y = x^2; endMultiple inputs and outputs:
function [sum_val, diff_val] = add_subtract(a, b) sum_val = a + b; diff_val = a - b; endFunction with no outputs:
function plot_data(x, y) figure; plot(x, y); xlabel('x'); ylabel('y'); end- Note:
MATLAB functions must be saved in .m files. The filename should match the function name.
script¶
- Purpose:
Store a sequence of statements.
- Syntax:
% filename: my_script.m
% This is a script file
statements
- Description:
Scripts are .m files that contain a sequence of MATLAB statements. They execute in the base workspace and can access and modify variables there.
- Examples:
Simple script (saved as
analysis.m):% Analysis script load data.mat; results = process_data(data); plot(results); save results.mat results;Script with calculations:
% Calculate beam deflection L = 10; % beam length E = 200e9; % Young's modulus I = 1e-4; % moment of inertia q = 1000; % distributed load % Maximum deflection w_max = q * L^4 / (8 * E * I); fprintf('Maximum deflection: %.2e m\n', w_max);- Note:
Scripts share the workspace with the command line. Variables created in scripts remain accessible.