Laboratory Lesson 15
Laboratory Lesson 15
MatLab Basics.
The procedure for performing work in the classroom:
1. Create your own folder to work in Matlab.
2. Call Matlab.
3. Set your own folder as a working folder instead of the standard Matlabwork
folder (by default), changing the contents of the Current Directory line at the top.
4. Familiarize yourself with the Matlab windows. Using the View command,
learn to remove and add windows.
5. Go to the Windows Command window. Calculate:
- the sum and difference of any two numbers without assigning values to variables;
- the product and quotient of dividing any two numbers;
- with the assignment of values to variables;
- check the results of calculations 1/0 and 0/0.
6. View a list of elementary mathematical functions (Rewrite the names of
basic elementary functions in the protocol).
7. Calculate the specified functions (for an individual task, (see Table 1.1) and
record the results of the calculations in the protocol).
8. Organize the storage of the protocol of your work in your folder.
9. View a list of variables in the workspace.
10. Save the results of calculations by the formulas.
11. Clear the data workspace.
12. Load the values of variables stored in the file.
13. Clear the command window.
Hometask:
1. Learn the purpose of the Command Windowsof MatLab.
2. Learn command is used to set the format of numbers.
3. Make a list the main MatLab system variables.
SIW task:
1. Think about what the results would be for the following expressions, and
then type them to verify your answers:
4^2–1
4 ^ (2 – 1)
2\3
5 * 2 – 9/3
5 – – 3
2. Use the help function to find out what the rounding functions fix, floor, ceil, and
round do. Experiment with them by passing different values to the functions: some
negative, some positive, some with fractions less than 0.5 and some greater. It is very
important when testing functions that you thoroughly test by trying different kinds of
arguments!
3. Calculate the range of integers that can be stored in the type int16. Use intmin and
intmax to verify your results.
4. Enter an assignment statement and view the type of the variable in the Workspace
Window. Then, change its type.
Control questions:
1. What is the MATLAB workbench?
2. What is the purpose of the window Command Windows?
3. What command is used to set the format of numbers?
4. What is the purpose of the service keys , ?
5. List the main MatLab system variables.
MatLabcommands.
Hometask:
1. Write a script to calculate the area of a rectangle. Be sure to comment the script.
2. Create a script that would prompt the user for a temperature, and then ‘F’ or ‘C’,
and store both inputs in variables. For example, when executed it would look like this
(assuming the user enters 85 and then F):
Enter the temperature: 85
Is that F or C?: F
3. Write a script to prompt the user for a character and number, and print the character
in a field-width of 4 and the number left-justified in a field width of 5 with two
decimal places. Test this by entering numbers with varying widths.
SIW task:
1. The atomic weight is the weight of an atom of a chemical element. For
example, the atomic weight of oxygen is 15.9994 and the atomic weight of hydrogen
is 1.0079.
2. Write a script that will calculate the molecular weight of hydrogen peroxide,
which consists of two atoms of hydrogen and two atoms of oxygen. Include
comments in the script. Use help to view the comment in your script.
3. Write an input statement that will prompt the user for a string. Then, find the
length of the string.
4. Write an input statement that will prompt the user for a real number, and store
it in a variable. Then, use the fprintf function to print the value of this variable using
two decimal places.
5. Experiment, in the Command Window, with using the fprintf function for real
numbers. Make a note of what happens for each. Use fprintf to print the real number
12345.6789
- without specifying any field width10 with four decimal places
- in a field width of 10 with two decimal places
- in a field width of 6 with four decimal place
- in a field width of 2 with four decimal places
6. Experiment, in the Command Window, with using the fprintf function for
integers. Make a note of what happens for each. Use fprintf to print the integer 12345
- without specifying any field width
- in a field width of 5
- in a field width of 8
- in a field width of 3
Control questions:
1. How can I get information about built-in elementary functions?
2. Give examples of the mathematical functions of the MatLab system.
3. How is the diary command used?
4. What is the feature of storing variables in files with a mat extension?
√
3
3 −5 3 10 |xk|
2
ax 3−b i=5;b=2.35;
y=tgik +10 e + − .
(a+b )2 ( a+b )2 a=25.2; x=0.1;k=−2.
4
y=
√ |c−d|+(a+c )2 −3 ix |c−d|+a
+10 e − 3
2
.
a=−1.25;d=2.5;i=5;
c=0.05; x=1.35.
sin 2 i √( a+c )2
5 ln|kx| 4
2 10 a−b 3 a=0.93;b=5.61;
y= − √|x −a |− + √ x−a2 +c3 x .
sin 7 cos kx c=0.31; x=−2.5;
k=2.
6
y=10 4
ax a−b ln 3
−| |+ 3 −e−kx b=0.35;
b 2 kx √ax 2+b 2 a=3.5; x=1.523;k=−2.
√ a=1.7;b=−1.25;
7 |b−a| 5 abc 0 . 7 abc
y=− +10 4 √|cos kx|+ − .
kx 2 . 4 sin7 c=−0.3; x=2.5;k=3.
√
8 a−x 4 3 a−kx
2
cos kx 2 bc a=−1.3;b=0.91;
y=sin +10 + −
c 2b tg3 ax c=0.75; x=2.32;k=8.
9
√
3
ln x+a2 2 10
4
c c=1.52;
y= −|0. 47 x − cos 2 k|− a=−2. 4; x=0.29; k=3.
0 . 47 x 2 7 x
10 2 2 a 2.5; b 1.35; i 3;
1. 5(a−b ) i ( a+x )cos 7
y= + +10 √|a−b|− 2 2
3
|a−b|c 5 ix +a bc c 0.72; x 2.75.
11 2
( x−d )( x +b )
2
cos kx d=1.25;b=0.75;n=4;
y=− +10−3 tgkn− .
√ x 2+b 2−cd
3 sin 5 c=2.2; x=0.32; k=2.
√
12 cos i ax 2 +|d| 46 kx d=−0.01;b=1.25;
y=− + −10 .
sin kx (a+b)2 (a+b )2 a=4.72;i=2;
x=2.25;k=3.
13 3 4
− 4 ( x+a) +x d
5
|x + a| √ d=0.95 ;b=0.05; a=−3.25;
y=cos k ( x−a )+10 + . x=8.2;k=4.
3 2. 4 b
k ( x−a )
14 e +c
kx 2
c=1.72;b=−0.31;a=2.01;
y=√|ax −b |+ ln kx −
5 2 3
−10−3 √ 2157 .
sin kx x=0.48;k=3.
15 1
y= −10 e +cos √ (x + b )+
−4 kx 2 √ x 2+ b
+
x=2.5;b=0.04;
9 0. 4 x k=3;n=5.
sin 3
+ 2 .
( x + b) n
3 2
y=√ x +c 2 ;x=e mk ;c=cos2 m+k 2
16 k =2 ;
m=1.8
17 а =7 ;
b=2.3
18 а =2 ;
p=1.6
19 2 2 х =1.52 ;
y=asin b+b cos a ; ; c=5
20 t=4.1 ;
p=3
21 m=2 ; x=1.1
22 k =7.2 ;
x=0.5
23 b=3 ;
c =1.7
24 t=2.2 ;
b=3
25 х =11 ;
p=2.6
Control questions:
1. What would happen if you use the name of a function, for example sin, as a
variable name?
2. Subscript indices must either be real positive integers or logicals.
3. What would happen if you reversed the order by mistake, and typed the following:
rem(5,13)
4. There is no built-in constant for e (2.718), so how can that value be obtained in
MATLAB?
5. What would happen if you go beyond the range for a particular type?