math lab Flashcards

1
Q

when is semicolon used

comment symbol

A

end of statement and when you want to hide the Matlab output for a expression

x=5; y = x+5 x=5 is not shown

% symbol

Dealing with Matrices and Arrays
2-D and 3-D Plotting and graphics
Linear Algebra
Algebraic Equations
Statistics
Data Analysis
Calculus and Differential Equations
Numerical Calculations
Integration
Transforms
Curve Fitting
Special Functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

MATLAB difference compared to other programs

A

While other programming languages mostly work with numbers one at a time, MATLAB is designed to operate primarily on whole matrices and arrays.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

addition, subtraction, multiplication left and right division, exponentiation

A

+, -, *, /(5/3) (3\5) same value ^

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

special variables and constants

A

ans: most recent answer
eps: floating-point representation
I,j (imaginary unit root of -1)
inf - infinity
NaN (undefined numerical not a number)
pi = pi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is who command and whos command

A

displays all the variable names we use

whos : gives size, bytes class,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

clear x
clear
clc

print in matlab

A

it will delete x
deletes all variables
clears all text from the command window

print in matlab

fprintf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Command Window
MATHLAB editor
workspace
command history
command directory

A

Command Window
the main area where commands can be entered at the command line. It is indicated by the command prompt (»).

MATLAB Editor. This is where you can write, edit, and save your scripts and functions.

commands can be done there and

command window: where all the editing happens variable assignment happens

workspace:
The workspace shows all the variables created and/or imported from files.

command history:
This panel shows or return commands that are entered at the command line.

command directory:
View Folders and m-files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

format short
format short e
format long
format bank
format rat

A

displays numbers with four decimal places
format short
7+10/3+5^1.2
x=17.232

format shorte
the command allows displaying in exponential form with 4 decimal places plus the exponent

and = 2.2922e+01

format long
15 digits after the decimal point

format bank
rounds number to 2 decimal places

format rat
gives closest rational expression resulting from a calculation
format rat
4.678*4.9
= 34177/1491

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

two types of vectors

A

row vectors and column vectors
row vector: X =[7,8,9,10]
X= 7 8 9 10

column vector X = [7; 8; 9; 10]

7
8
9
10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

matrix

A

2D array numbers,
A = [1 2 3 4; 4 5 6 7; 7 8 9 10 ]

1 2 3 4
4 5 6 7
7 8 9 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

elementary math built

trigonometry function

other functions

A

sqrt(x), nthroot(x,n) n here is the root value, 2,3….=> real nth root of a real number 25 has 2 roots
exp(x), abs(x), log(x), log10(x), factorial(x)

trigonometry function
sin(x) => radians, sind(x) => degree
cos(x), cosd(x), tan(x), tand(x)
cot(x), cotd(x)
asin(x), asind(x), acos(x), acosd(x)

other function

round(x) (nearest integer), fix(x)=> Round towards 0, other words, chops fraction part

ceil(x) => round towards positive infinity
ceil(27/2) => 14
floor(x) => rounds minus infinity
rem(x,y) remainder after x/y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

example question y=e^a*sin(x) + 10root(y)
if a=5, x=2, y=8

A

a=5; x=2; y=8;
y=exp(-a)sin(x)+10sqrt(y)
y=2.829039804

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

special type of arrays

A

four types of arrays are:
zeros(), +> zero array zeros(5) => 5 by 5
eye() => returns 1 diagonal towards left to right
ones(), => ones(x,y)
rand() => random functions with given range
ex numbers on (0,1)
rand(3,5)
0.8147 0.9134 0.2785 0.9649 0.9572
0.9058 0.6324 0.5469 0.1576 0.4854
0.1270 0.0975 0.9575 0.9706 0.8003

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

~= operator

logical operators symbol

A

determine inequality, like a not function

&. |, ~(not)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what are M-files and what is a script file

A

.m at the end of their name
ASCII text files
two M-files: Script files: and function files

script files:A script file contains a set of valid commands, just like you would enter directly into the Command Window.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to create a function file matlab

A

A function is a group of statements that together perform a task.

In MATLAB, functions are defined in separate files.
Functions can accept more than one input arguments and

 may return more than one output arguments. The syntax of the function definition line is as follows: 

Note that the function_name must be the same as the file name
(without the m.extension) in which the function is written.

function [sum,prod]= my name (a,b,c)
sum = a+b+c;
prod=abc
end

on command window
For,&raquo_space; myname(20,21,22), the output is: ans =63
For,&raquo_space; [s,p]=myname(20,21,22), the output is: s = 63, p =9240

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

for loop syntax

while loop syntax

all conditional statements

A

repeat a statement or a group of statements for a fixed number of times.

for m=1:3
num=1/(m+1)
end
OUTPUT:
num = 0.5000
num = 0.3333
num = 0.2500

while loop:
repeatedly executes statements while condition is true.

> > a = 10;
while(a < 15)
fprintf(‘value of a: %d\n’, a);
a = a + 1;
end
%d is a format specifier, d in this case is integer
\n is next line

all conditional statements

if-end; if-else-end; if-elseif-elseif-else-end; switch case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

switch mat lab

A

is used to test for the equality against a set of known values

 >> grade = 'B'; 
           switch(grade) 
           case 'A' 
                    fprintf('Excellent!\n' ); 
           case 'B' 
                      fprintf('Well done\n' ); 
           case 'C' 
                      fprintf('Well done\n' );
           case 'D' 
                      fprintf('You passed\n' );
            case 'F' 
                          fprintf('Better try again\n' ); 
             otherwise 
                           fprintf('Invalid grade\n' );    end
19
Q

plot y=x^2

A

The script file is as follows:
&raquo_space; x = [-100:20:100]; 20
is increments, and the others are the range
» y = x.^2; [Here the “dot” is necessary]
&raquo_space; plot(x, y)

20
Q

3D graph examples

A

Let us create a 3D surface map for the function 𝑧=𝑥𝑒^(−(𝑥^2+𝑦^2 ) )
The script file is as follows:
&raquo_space; [x,y] = meshgrid(-2:0.2:2);
&raquo_space; z = x.* exp(-x.^2 - y.^2);
&raquo_space; surf(x,y,z)

surf to create a surface plot

mesh grid points over the domain of the function(the range of values)

21
Q

another 3d graph examples

A

Let us consider another example to create a 3D surface map for the function 𝑧=sinx+cosy
The script file is as follows:
&raquo_space; [x,y] = mesh grid(-2:0.2:2); these are coordinates
&raquo_space; z=sin(x)+cos(y);
&raquo_space; surf(x,y,z)

surf to create a surface plot

mesh grid points over the domain of the function(the range of values)

22
Q

xlabel and y label commands for x, y axis for graphs

A

> > x = [0:0.01:10];
&raquo_space; y = sin(x);
&raquo_space; plot(x, y), xlabel(‘x’), ylabel(‘Sin(x)’), title(‘Sin(x) Graph’)

23
Q

polar coordinates

A

polarplot(theta,rho). Here thetais the angle in radians andrhois the radius value for each point/height of sin cos wave combo

> > theta = 0:0.01:2pi;
&raquo_space; rho = sin(2
theta).cos(2theta);
&raquo_space; polarplot(theta,rho)

24
Q

another example of polar coordinates

A

Plot the curve 𝑟=1−𝑠𝑖𝑛𝜃
The script file is as follows:
&raquo_space; theta = 0:0.01:2*pi;
&raquo_space; rho = 1 - sin(theta);
&raquo_space; polar(theta, rho)

polarplot(theta,rho). Here thetais the angle in radians andrhois the radius value for each point/height of sin cos wave combo

25
Q

parametric curve

A

fplot3(xt,yt,zt), whichplots the parametric curve xt=x(t), yt=y(t), and zt=z(t) over the default interval−5<𝑡<5

Let us plot a parametric curve: x=sin(t); y=cos(t); z=t over the default parameter range[-5 5].

> > syms t
xt = sin(t);
yt = cos(t);
zt = t;
fplot3(xt,yt,zt)

26
Q

parametric plots, with different range

A

Plot the parametric curve 𝑥=𝑒^(−𝑡/10)sin⁡(5𝑡);
𝑦=𝑒^(−𝑡/10)cos⁡(5𝑡);
z=t;
[-10 10]

   >> syms t 
   >> xt = exp(-t/10).*sin(5*t); 
   >> yt = exp(-t/10).*cos(5*t); 
   >> zt = t; 
   >> fplot3(xt,yt,zt,[-10 10])
27
Q

Radius of curvature

A

RADIUS FORMULA
d=(1+G(x,y)^2)^3/2))/c

rho=d/c

Find the radius of curvature of the curve 𝑥^(2/3)+𝑦^(2/3)=𝑎^(2/3) at any point (x,y) of the curve.
» syms x y a
» F(x,y)=x^(2/3)+y^(2/3)-a^(2/3);
dy_dx = - diff(F,x)/diff(F,y)
Out put: dy_dx(x, y) =-y^(1/3)/x^(1/3)
» G(x,y)=-y^(1/3)/x^(1/3);
» a=diff(G,x);
» b=diff(G,y);
» c=a+bG(x,y)
Out put: c(x, y) =
1/(3
x^(2/3)y^(1/3)) +
y^(1/3)/(3
x^(4/3))

> > simplify(c)
Out put: (x, y) =(x^(2/3) + y^(2/3))/(3x^(4/3)y^(1/3))
d=(1+G(x,y)^2)^(3/2)
Out put: d =(y^(2/3)/x^(2/3) + 1)^(3/2)
rho=d/c
Out put: rho(x, y) =(y^(2/3)/x^(2/3) + 1)^(3/2)/(1/(3x^(2/3)y^(1/3)) + y^(1/3)/(3x^(4/3)))
simplify(rho(x,y))
Out put: (y^(2/3)/x^(2/3) + 1)^(3/2)/(1/(3
x^(2/3)y^(1/3)) + y^(1/3)/(3x^(4/3)))

28
Q

Radius of curvature using theta

A

theta formula
rho = (r^2+(r^2’’))
^3/2/(r^2+2r^2’‘-r*r^2’’)

syms theta
» r=exp(2theta);
» r1=diff(r,theta);
» r2=diff(diff(r,theta));
» a=(r^2+r1^2)^(3/2)
Out put: a =(5
exp(4theta))^(3/2)
» b=r^2+2
r1^2-rr2
Out put: b =5
exp(4theta)
» rho=a/b
Out put: rho =(exp(-4
theta)(5exp(4theta))^(3/2))/5
simplify(rho)
Out put: 5^(1/2)
exp(4*theta)^(1/2)

29
Q

why is a dot used in graph plotting and numerical integration

A

scaler: single value,
vector: 1D-array

MATLAB can only do multiplication on matrix, when ever there is a vector quantity u need to put a dot

Dots are used in MATLAB to specify element-wise operations when working with vectors or matrices.
.*, .^, ./

special case:
trigonometric functions like neither vectors nor matrices themselves. Instead, they are element-wise functions

and exp also

but exp(-x.^2 - y.^2);
inside u should still but dot for these
and sin(-x.^2 - y.^2);

Imagine each will give u a matrix value, if its one
matrix set exp and trig functions can work, but if its multiple u have to put dot

30
Q

partial derivative of a function, syntax for fxy

A

f=sin(𝑥)+𝑦^3+𝑥^10−𝑦^2+log⁡(𝑥), then find 𝑓𝑥 & 𝑓𝑦.
» syms x y
» f=sin(x)+y^3+x^10-y^2+log(x);
» diff(f,x)
» diff(f,y)
syntax
»diff(f,x,y)
»diff(f,y,x)
Out put: f =log(x) + sin(x) + x^10 - y^2 + y^3
ans =cos(x) + 1/x + 10x^9
ans =3
y^2 - 2*y

31
Q

double partial derivation

A

f=𝑥^2+2∗𝑦^2−22,then find 𝑓𝑥𝑥 & 𝑓𝑦𝑦
&raquo_space; syms x y
&raquo_space; f=x^2+2y^2-22
&raquo_space; diff(f,x,2)
&raquo_space; diff(f,y,2)
Out put: f = x^2 + 2
y^2 – 22
ans =2
ans=4

32
Q

Taylor and maclaurin

A

Expand f(x)=𝑒^𝑥𝑠𝑖𝑛𝑥 about the point x=2 up to third degree terms.
» syms x
» f = exp(xsin(x));
» t= taylor(f, ‘ExpansionPoint’, 2, ‘Order’, 3)
Out put:
t=exp(2
sin(2)) + exp(2sin(2))(2cos(2) + sin(2))(x - 2) + exp(2sin(2))(x - 2)^2(cos(2) - sin(2) + (2cos(2) + sin(2))*(cos(2) + sin(2)/2))

33
Q

Taylors Theorem theorem without giving a value only degree

A

Expand f(x)=log(secx) about the origin up to six degree terms.
» syms x
» f = log(sec(x));
» T= taylor(f, ‘Order’, 7)
Out put:
T = x^6/45 + x^4/12 + x^2/2

34
Q

taylors plot graphs

A
  1. sinx: &raquo_space; t = [0:0.1:2*pi]
    &raquo_space; a = sin(t);
    &raquo_space; plot(t,a)
  2. cosx: &raquo_space; t = [0:0.1:2*pi]
    &raquo_space; a = cos(t);
    &raquo_space; plot(t,a)
35
Q

taylors f(x,y)

A

Expand f(x,y)=e^x𝑐𝑜𝑠𝑦 about the point 𝑥=1,𝑦=𝜋/4 up to three degree
terms.
&raquo_space; syms x y
&raquo_space; f=exp(x)cos(y);
&raquo_space; t = taylor(f, [x, y], [1, pi/4], ‘Order’, 3)
Out put:
T = (2^(1/2)
exp(1))/2 - (2^(1/2)exp(1)(y - pi/4)^2)/4 + (2^(1/2)exp(1)(x - 1)^2)/4 - (2^(1/2)exp(1)(y - pi/4))/2 + (2^(1/2)exp(1)(x - 1))/2 - (2^(1/2)exp(1)(y - pi/4)*(x - 1))/2

36
Q

f(x,y)=0

A

Expand 𝑓(𝑥,𝑦)=𝑒^𝑦 log⁡(1+𝑥) about the origin up to fourth degree
terms.
&raquo_space; syms x y
&raquo_space; f=exp(y)log(1+x);
&raquo_space; T= taylor(f, [x, y], ‘Order’, 4)
Out put:
T= x^3/3 - (x^2
y)/2 - x^2/2 + (xy^2)/2 + xy + x

37
Q

why is syms used

and why is ode used

A

syms command is used to define symbolic variables. These are variables that represent mathematical symbols rather than numeric values. Symbolic variables allow you to perform symbolic computations, such as differentiation, integration, algebraic manipulation,

ODE in MATLAB refers to solving Ordinary Differential Equations numerically or symbolically. MATLAB offers various built-in functions and tools for handling ODEs.

38
Q

differential equations and boundary condition

A

𝑑𝑦/𝑑𝑡=𝑡𝑦
» syms y(t)
» ode = diff(y,t) == ty;
» ySol(t) = dsolve(ode)
Output: ySol(t) =C1
exp(t^2/2)

Solve the differential equation: 𝑑𝑥/𝑑𝑡=𝑥+𝑡, given 𝑥(0)=0.
» syms x(t)
» ode = diff(x,t) == x+t;
» xSol(t) = dsolve(ode)
Output: xSol(t) =C1*exp(t) - t – 1.
To find the value of C1, we use:
» cond = x(0) == 0;
» xSol(t) = dsolve(ode,cond)
Output: ySol(t) =exp(t) - t - 1

39
Q

PDE arbitrary constants:
z=(x-a)^2+(y-b)^2

A

syms x y a b p q
z=(x-a)^2+(y-b)^2;
eq1=p==diff(z,x)
c1=solve(eq1,a)
eq2=q==diff(z,y)
c2=solve(eq2,b)
pde=subs(z,a,c1)
pde=subs(pde,b,c2)

40
Q

arbitrary another example
2z=x^2/a^2+y^2/b^2

A

syms x y a b p q
z=x^2/(2a^2)+y^2/(2b^2)
eq1=p==diff(z,x)
c1=solve(eq1,a)
eq2=q==diff(z,y)
c2=solve(eq2,b)
pde=subs(z,a,c1)
pde=subs(pde,b,c2)

41
Q

Plot the graph of solution of first order differential equations

𝑑𝑥/𝑑𝑡=𝑥+𝑡, given 𝑥(0)=0.

A

𝑑𝑥/𝑑𝑡=𝑥+𝑡, given 𝑥(0)=0.

> > tspan=[0 2]; % specify time span
x0=0; % specify x0
[t,x]=ode45(@(t,x)x+t,tspan,x0);
% now execute ode45
disp([t,x])
𝑡 is a vector with all discrete points ; and 𝑥 contains the values of the variable 𝑥.
plot(t,x) % plot t verses x

42
Q

(𝑥^2−1) 𝑑𝑦/𝑑𝑥+2𝑥𝑦=1, given 𝑦(0)=1.

A

(𝑥^2−1) 𝑑𝑦/𝑑𝑥+2𝑥𝑦=1, given 𝑦(0)=1.
» y0=1;
» x0=0;
» xend=[1,5,10]; same for all values,
This specifies the end points for x
x where the solution will be evaluated.
» xspan=[x0,xend];
Combines the start (x0) and end points (xend) into an array, defining the intervals over which the ODE is solved.
» [x,y]=ode45(@(x,y)(1-2xy)/(x^2-1),xspan,y0);
» disp([x,y])
» plot(x,y)

43
Q

non-linear differential equation

(dy/dt+y)^2=1, y(0)=0

A

> > syms y(t)
ode = (diff(y,t)+y)^2 == 1;
ySol(t) = dsolve(ode)
Output: ySol(t) =
C1exp(-t) + 1; C2exp(-t) – 1
To find the values of C1 and C2, we use:
cond = y(0) == 0;
ySol(t) = dsolve(ode,cond)
Output: ySol(t) =exp(-t) – 1; 1 - exp(-t)

44
Q

radius of curvature of the parametric curve

A

Formula of Parametric Curve
(x1^2+y1^2)^3/2/
(x1y2-y1x2)

=6𝑡^2−3𝑡^4,
𝑦=8𝑡^3.
» syms t
» x=6t^2-3t^4;
» x1=diff(x,t);
» x2=diff(diff(x,t));
» y=8t^3;
» y1=diff(y,t);
» y2=diff(diff(y,t));
» a=(x1^2+y1^2)
Out put: a =(- 12
t^3 + 12t)^2 + 576t^4
» b=simplify(a)
Out put: b =144t^2(t^2 + 1)^2
» c=(x1y2)-(y1x2)
Out put: c =48t(- 12t^3 + 12t) + 24t^2(36t^2 - 12)
» d=simplify(c)
Out put: d =288
t^2(t^2 + 1)
» e=b^(3/2)
Out put: e =(144
t^2(t^2 + 1)^2)^(3/2)
»rho=e/d
Out put: rho =(144
t^2(t^2 + 1)^2)^(3/2)/(288t^2(t^2 + 1))
»simplify(rho)
Out put: rho= (6
(t^2(t^2 + 1)^2)^(3/2))/(t^2(t^2 + 1))