Friday, October 20, 2023

Introduction to MATLAB

When you start MATLAB, the desktop appears in its default layout.
      

Table 1: Basic Arithmetic Operations

Sl. No.

Input

Output

Sl. No.

Input

Output

1

2+3

5

5

x^2

25

2

x=5; y=3; x+y

8

6

sqrt(y)

1.7321

3

x*y

15

7

x/0

Inf

4

x/y

1.6667

8

(x+y)/(x^2+y^2)

0.2353

 

Table 2: Basic Functions

Sl. No.

Input

Output

Sl. No.

Input

Output

1

x=2; y=pi;

 

8

nthroot(-4,3)

-1.5874

2

sin(y/4)

asin(0.7071)

0.7071

0.7854

9

nthroot(-4,2)

Error: Error using nthroot

If X is negative, N must be an odd integer.

asin(x)= inverse sine

Output will be displayed in terms of radians

Syntax nthroot(X,N)

3

exp(x)

7.3891

10

z=sqrt(-4)

0.0000 + 2.0000i

4

log(x)

0.6931

11

imag(z)

2

5

z=log10(x)

z=0.3010

12

real(z)

0

6

format long

z

z = 0.301029995663981

13

abs(z)

2

7

format short

cosh(y)

11.5920

14

abs(-4)

4

# Note: If you end a statement with a semicolon, MATLAB performs the computation, but suppresses the display of output in the Command Window.

Table 3: Basic Commands

Sl. No.

Command

Output

1

clc

Clears all the text from the Command Window, resulting in a clear screen. (Does not delete the variables created)

2

clear

Removes all variables from the current workspace, releasing them from system memory. (Does not clear the screen)

3

close all

Closes all figures whose handles are visible

4

syms

creates symbolic scalar variables

Example

Input

Output

sin(x)

Error: Unrecognized function or variable 'x'.

syms x y

sin(x)+exp(y)

sin(x)+exp(y)

5

vpa

Variable-precision arithmetic  evaluate each element of the symbolic input x to at least d significant digits

Input

Output

a=((1 + sqrt(sym(5)))/2)

a=5^(1/2)/2 + 1/2

vpa(a)

1.6180339887498948482045868343656

vpa(a,3)

1.62

6

simplify

Performs algebraic simplification

Input

Output

syms x

sin(x)^2+cos(x)^2

cos(x)^2 + sin(x)^2

simplify(cos(x)^2 + sin(x)^2)

1

7

pretty

Prints expression in a plain-text format that resembles typeset mathematics.

Input

Output

syms x y

z=(y^2 + x^2)/(x*y)

z=(y^2 + x^2)/(x*y)

pretty(z)

  2     2

y + x

-------

x y

*For true typeset rendering, use Live Scripts instead

8

subs

subs(s, old, new) returns a copy of s, replacing all occurrences of old with new, and then evaluates s.

Input

Output

syms x y

subs(x + y,x,4)

4+y

f(x,y) = x + y;

f = subs(f,[x,y],[a,b])

b + a

9

input

input(prompt) displays the text in prompt and waits for the user to input a value and press the Return key.

Input

Output

x=input( Enter the value of x: )

Enter the value of x: _

10

fprintf

Formats data and displays the results on the screen

Input

Output

a=1;

b=1.45;

f=x+y;

fprintf( a=%d, b=%f, f=%s , a,b,f)

a=1, b=1.450000, f=x + y

To format numeric and character data use the following

%d - Integer, %f - Floating-point, %s - String

To format the display use the following

\t - tab space, \n - next line

11

disp

Disp(x) displays the value of variable x without printing the variable name

Input

Output

a=1

disp(a)

disp( a is equal to + a)

a=1

1

a is equal to 1

12

diff(f,var,n)

Differentiate the given function n times with respect to variable var

Input

Output

syms f(x)

f(x)=sin(x);

diff(f)

 

cos(x)

syms f(x)

f(x)=sin(x);

diff(f,x)

cos(x)

syms f(x)

f(x)=sin(x);

diff(f,x,2)

-sin(x)

13

int(f,var,a,b)

Evaluate the definite integral of the function f from a to b

Input

Output

syms f(x)

f(x)=sin(x);

int(f)

 

-cos(x)

syms f(x)

f(x)=csc(x);

int(f,x)

log(tan(x/2))

 

*csc(x)=cosec(x)

syms f(x)

f(x)=sin(x);

int(f,x,0,pi/2)

 

1

 

Table 4: Vectors and Matrices

Sl. No.

Input

Output

1

x=[1,2,3,4,5,6]

x = 1 2 3 4 5 6

2

x=1:1:6

x = 1 2 3 4 5 6

3

x=1:6

x = 1 2 3 4 5 6

4

x=linspace(1,6,5)

x = 1.0000 2.2500 3.5000 4.7500 6.0000

5

y=x+1

y = 2.0000 3.2500 4.5000 5.7500 7.0000

6

y=x*2

y = 2.0000 4.5000 7.0000 9.5000 12.0000

7

a=[1,2,3,2,1]

 

z=a*x

 

 

a = 2 2 2 2 2

Error: Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.

a and x are 1x5 matrices hence multiplication is not possible

8

z=a.*x

z = 1.0000 4.5000 10.5000 9.5000 6.0000

( .* represents elementwise multiplication, i.e., it will take

z = 1*1 2*2.25 3*3.5 2*4.75 1*6 )

9

y=x/2

y = 0.5000 1.1250 1.7500 2.3750 3.0000

10

y=2/x

Error: Matrix dimensions must agree.

(The number 2 will be interpreted as a 1x1 matrix, making regular division unfeasible. Use element-wise division instead.)

11

y=2./x

y = 2.0000 0.8889 0.5714 0.4211 0.3333

12

y=a./x

y = 1.0000 0.8889 0.8571 0.4211 0.1667

13

length(x)

6

14

M=[1 2 3;3 4 5;6 7 8]

1 2 3

3 4 5

6 7 8

15

N=[1 3 5;-1 4 6;-3 4 -2]

1 3 5

-1 4 6

-3 4 -2

16

M+N

2 5 8

2 8 11

3 11 6

17

M*N

-10 23 11

-16 45 29

-25 78 56

18

b=ones(1,3)

1 1 1

19

x=zeros(3,4)

0 0 0 0

0 0 0 0

0 0 0 0

20

x*b

Incorrect dimensions for matrix multiplication.

x is a 3x4 matrix and b is a 1x3 matrix hence multiplication is not possible

21

b*x

0 0 0 0

22

2*b.*N

2 6 10

-2 8 12

-6 8 -4

22

size(x)

3 4

23

det(N)

-52

24

inv(N)

0.6154 -0.5000 0.0385

0.3846 -0.2500 0.2115

-0.1538 0.2500 -0.1346


Working with Script file (.m file)

<SScripts consist of sets of MATLAB commands that are kept in basic text files.

<]>Script files must end with the extension '.m' (for example 'myScript.m'), and often these files are referred to as m-files.

<>> Essentially, m-files run a sequence of MATLAB instructions. Alternatively, they can serve as functions capable of receiving inputs, generating multiple outputs.



Working with Live Script file (.mlx file)

<!>Live scripts and live functions are interactive documents that combine MATLAB code with formatted text, equations, and images in a single environment called the Live Editor. In addition, live scripts store and display output alongside the code that creates it.

<!>To create a live script: MATLAB>Home>Live Script



No comments:

Post a Comment

Search This Blog