Professional Documents
Culture Documents
1.2 VARIABLE
Variable can be defined as an address to a piece of computer memory where the variable value (or data) is
stored. When the variable get a new value, it replace it old value with the new one.
There are important rules in Matlab for naing a variables:
1. All names must start with a letter.
2. You can check how many characters you can use for one variable name using the command: namelengthmax
3. The allowable characters for variable name are letters, numbers, and the underscore.
1-1
1-2
4. To check whether the variable name is valid or not, use the command: isvarname.
5.Names are case sensitive. The variable x is different from the variable X
6. MATLAB has keywords. Don’t use them as variables: You can list these keywords using command:
iskeyword.
The type (or class) of a variable usually reflects the type of its value, here are some and students are required
to carefully review the other types:
1. 1. Numeric variable:
(a) double, single precision which are required for fractional data. Use realmin and realmax
to show range of values.
(b) integer: Use for signed and unsigned whole numbers.More efficient use of memory. Use intmin
and intmax to show range of values. Choose from 4 sizes (8, 16, 32, and 64 bits) using int8,
uint8, int16, int16, int32,uint32, int64, uint64.
2. String (character) variable: Data type for text. For multiple character arrays, use cell arrays.
3. Logical variable: Use in relational conditions or to test state. Can have one of two values: true or
false.
There are three main structure for the variable value (data) stored in the memory: scalar, vector, tensor. We
will discuss variable structure later on.
On the other hand, the constant refer to a value in the computer memory that does not change. There are
several constants build in Matlab such as pi.
1.4.1 Scalar
A single value represented as a 1 x 1 matrix.
𝑎 = 4, In Matlab ⇒ a=4
1-3
Input is a powerful tool in matlab used to enter a value to a scalar in the program. The run will pause until
the user enter a value and press enter.
1.4.2 Vector
A list of values, arranged in either a column or a row, is a one-dimensional matrix.
3
𝑄 = 5 In Matlab ⇒ Q=[3;5;8]
8
1.4.3 Tensor
A list of values, arranged in columns and rows.
2 1 9
B = 7 1 3 In Matlab ⇒ B=[2,1,9;7,1,3;6,2,5]
6 2 5
The arithmetic operations between matrices are listed in the table below.
• Comparisons are either true or false, and most computer programs (including MATLAB) use the number
1 for true and 0 for false.
• WARNING: look at the difference between the equals operator (= =) and the assignment operator (=).
3. The elseif Structure The elseif function allows you to check multiple criteria while keeping the code
easy to read.
if condition 1
statements 1
elseif condition 2
statement 2
else
statement n
end
1.8 LOOPS/ITERATION
Example 1.8.2 k = 0;
while k<3
k = k+1
end
𝑥2 𝑥3 𝑥4 𝑛
Example 1.8.3 Find 𝑓 = 𝑥 + 2 + 3 + 4 + ... 𝑥𝑛 for 10 terms. User inputs x through input command. Use
while loop.
clc;clear; k=0;
f=0;
x=input(’Enter the value of x’);
while k<10
k=k+1;
f=f+xk̂/k;
end
disp(’f(x) is’)
disp (f)
Í (−1) 𝑘+1
Example 1.8.4 Example: Find the natural log of x, where Log(x)= 𝑛𝑘=0 𝑘
clc;clear;
k=0;
f=0;
x=input(’Enter the value of x’);
while k<2000
k=k+1;
f=f+((-1)^(k+1))/k;
end
1-7
disp(’f(x) is’)
disp (f)
2. fprintf Performs formatted writes to screen or file. Slightly more complicated than disp. Provide
total control over the appearance of output.
To cause MATLAB to start a new line, you’ll need to use \n, called a linefeed, at the end of the string.
Example 1.9.1 Example: Write a MATALB program that convert feet [1,2,3] into inches and print the
results in a table.
feet = 1:3;
inches = feet.*12;
table = [feet;inches];
fprintf(’%4.0f %7.2f \n’,table);
1 12.00
2 24.00
3 36.00
1-8
fprintf function can be used to send formatted output to a file. First, you’ll need to create and open an output
file and assign it a file identifier (nickname). You do this with the fopen function.
file_id = fopen(’my_output_file.txt’, ’wt’);
file_id: name of the output file.
Example 1.9.2 Example: Write a MATALB program that convert feet [1,2,3] into inches and print the
results in an external file named my_output_file
feet = 1:3;
inches = feet.*12;
table = [feet;inches];
file_id = fopen(’my_output_file.txt’);
fprintf(file_id,’%4.0f %7.2f \n’,table);
fclose(file_id);
Example 1.11.2 define the function f(x)=x2+9 and plot it versus x where 1𝑥 ≤ 5 ≤ 5.
syms f(x);
f(x)=x2̂+9;
x1=1:0.1:5;
M=f(x1);
plot(x1,M)
Example 1.11.3 write program to define and plot the function 𝑔(𝑡) = 3𝑠𝑖𝑛2𝑡 + (𝑐𝑜𝑠3𝑡) 2
syms g(t);
g(t)=3*sin(2*t)+cos(3*t)^2;
t1=0:pi/100:2*pi;
g1=g(t1);
plot(t1,g1,’m-.ˆ
’)
title(’Plot of g(t)=3*sin(2*t)+cos(3*t)^2’);
xlabel(’t’);ylabel(’g(t)’)
1-11
1.12 DIFFERENCES AND APPROXIMATE DERIVATIVES
The derivative can be thought of as the slope of a function or as the rate of change of the function.
syms x y;
y=x^3-4*x^2+5;
dy=diff(y,’x’,2);
disp(dy);
-------
dy =
6*x - 8
To substitute a value into a function use subs() function. The sytanx of this function is:
syntax: subs(function, variable (or expression) , value (or expression))
1.13 INTEGRATION
let’s take the following example:
∫ 𝜋 ∫ cos 𝑥
4
Example 1.13.1 find the value of 𝑚 = 0 sin 𝑥
𝑦𝑑𝑦𝑑𝑥
clear; clc
syms x y m R
R(x,y)=y;
m=int(int(R,’y’,sin(x),cos(x)),’x’,0,pi/4);
you can see how to specify the integral limits, the function, and sequences of the two integrals.
∫ 2 ∫ √4−𝑥 2
Example 1.13.2 Example 2: Using Matlab, find 4 √
−2 − 4−𝑥 2
(4 − 𝑦 2 ) 1/2 𝑑𝑦𝑑𝑥
clear; clc
syms x y
Q=int(int((4-y^2)^0.5,’y’,-(4-x^2)^0.5,(4-x^2)^0.5),’x’,-2,2);
disp(Q)
1-12
1.14 GRADIENT, DIVERGENCE, CURL
One of the most important operator in differential calculus.
𝜕 𝜕 𝜕
∇= 𝑖+ 𝑗+ 𝑘
𝜕𝑥 𝜕𝑦 𝜕𝑥