M-3 Matlab
M-3 Matlab
M-3 Matlab
• Functions can accept more than one input arguments and may return
more than one output arguments.
Functions
• It is an M-file(having extension .m) like a script file(using editor
window)
• The variables in the function file are all local.
HA = @(x,y)2*x^2-4*x*y+y^2
>>HA(2,3)
ans = -7
function mainAdder()
a = 5;
b = 7;
result = subFunction(a, b);
disp(['Result: ' num2str(result)]);
end
• % Subfunction
function c = subFunction(x, y)
c = x + y;
end
In the "Environment" section, click on "Set Path." This will open the "Set Path"
Preferences window.
In the "Set Path" Preferences window, you can add or remove directories from the
MATLAB path.
Click "Save" to apply the changes and save the path preferences.
Program is partially running and showing
error in a particular line
• Inner matrix dimension must agree : Because of not following the
matrix rules, inner matrix dimension mismatch
• Undefined function or command : Incorrect command or
statement
while x <20
x = 3*x – 1
end
m : initial value
s : step (incremental) value
n : terminating value
for i = 2:2:4
x = i^2
end
fprintf(‘The answer is %d \n’ , x)
m:2
s :2
n:4
x = 7;
if x > 5
disp('x is greater than 5');
else
disp('x is not greater than 5');
end
y = 3;
if y > 5
disp('y is greater than 5');
elseif y == 5
disp('y is equal to 5');
else
disp('y is less than 5’);
end
if num > 0
disp([num2str(num) ' is a positive number.']);
elseif num < 0
disp([num2str(num) ' is a negative number.']);
else
disp([num2str(num) ' is zero.']);
end
Determine the
Design the
Start State the problem number of input
algorithm
and outputs
Convert the
Test the output of
algorithm into
the MATLAB Finish
MATLAB
Program
statements
Top –Down Design Techniques
• An algorithm is a step-by-step procedure for finding the solution of a
problem
• The standard forms that we used to describe algorithms are called
constructs
• These constructs are also known as pseudo codes (p-codes)
• P codes are the hybrid mixture of MATLAB and English
• Separate line is written for each distinct idea or segment
Pseudocode
Example: Write pseudocode for a MATLAB program which converts
temperature from degree Fahrenheit to Kelvins.
Prompt the user to enter temperature in degree Fahrenheit