Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

2, MATLAB Slides Week2 PPT

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

DEPARTMENT OF CIVIL ENGINEERING INDIAN INSTITUTE OF TECHNOLOGY BOMBAY

Introduction to MATLAB
Summer of Core - 2023

IIT Bombay

Saurabh Kumar Khandelwal


M Tech. Structural Engg.
IIT Bombay
(22m0574@iitb.ac.in)
Week 2
Mathematical Functions in Matlab
Elementary Mathematical Functions

 round(x): rounds x to the nearest integer

 floor(x): rounds x down to nearest integer

 ceil(x): rounds x up to the nearest integer

 rem(y,x): remainder after dividing y by x

 sign(x): returns -1 if x<0; returns 0 if x=0; returns 1 if x>0

 rand or rand(1): generates a random number between 0 and 1

 sqrt(x): square root function

 abs(x): absolute value function

 min(x), max(x)

 exp(x)
Logarithms Function

 log:  Natural logarithm

 log10: Log base 10

 log2: Base-2 logarithm  
Trigonometric Functions

  sin(x) 
  asin(x) ​

  cos(x)
  acos(x)​

  tan(x)
  atan(x)​

  cot(x)
  acot(x)​

  sec(x)
  asec(x)​

  csc(x)
  acsc(x)
Hyperbolic Functions
  sinh(x) 

  cosh(x)

  tanh(x)

  coth(x)

  sech(x)

  csch(x)
Matlab as a Programming Language
 MATLAB Scripts are saved as so-called .m files (file extension is .m).
When using the Script Editor, you may create several lines of code and execute all
in one batch. You can easily do changes in your code, create comments, etc.
Scripting and user-
defined functions in
MATLAB
Syntax

    function [y1,...,yN] = myfun(x1,...,xM)

 Valid function names begin with an alphabetic character and can contain letters,

numbers, or underscores.

The name of the file must match the name of the first function in the file.
 Function with One Output

 function ave = calculateAverage(x)

     ave = sum(x(:))/numel(x);

end

z = 1:99;

ave = calculateAverage(z)
 Function with Multiple Outputs

 function [m,s] = stat(x)

     n = length(x);

     m = sum(x)/n;

     s = sqrt(sum((x-m).^2/n));

end

values = [12.7, 45.4, 98.9, 26.6, 53.1];

[ave,stdev] = stat(values)
• Multiple Functions in a Function File

• function [m,s] = stat2(x)

•     n = length(x);

•     m = avg(x,n);

•     s = sqrt(sum((x-m).^2/n));

• end

• function m = avg(x,n)

•     m = sum(x)/n;

end

values = [12.7, 45.4, 98.9, 26.6, 53.1];

• [ave,stdev] = stat2(values)

You might also like