Lab1
docx
keyboard_arrow_up
School
The University of Tennessee, Knoxville *
*We aren’t endorsed by this school
Course
315
Subject
Mathematics
Date
Apr 3, 2024
Type
docx
Pages
6
Uploaded by kaygraced117
x=Lab 1: Introduction to Matlab 1. Creating vectors (a) Generate the following vectors: A = [1 0 4 5 3 9 0 2] a = [4 5 0 2 0 0 7 1] Be aware that Matlab are case sensitive. Vector A and a have different values. (b) Generate the following vectors: B = [A a] B=[ 1 0 4 5 3 9 0 2 4 5 0 3 0 0 7 1]
C = [a,A] C=[4 5 0 2 0 0 7 1 1 0 4 5 3 9 0 2]
Concatenation is the process of joining small matrices to make bigger ones. In fact, you
made your first matrix by concatenating its individual elements. The pair of square
brackets, [], is the concatenation operator. (c) Generate the following vectors using function zeros and ones: D = [0 0 0 . . . 0] with fifty 0’s. D=zeros(1,50)
E = [1 1 1 . . . 1] with a hundred 1’s. E=ones(1,100)
(d) Generate the following vectors using the colon operator F = [1 2 3 4 . . . 30] F=[1:30]
G = [25 22 19 16 13 10 7 4 1] G=[25:-3:1]
H = [0 0.2 0.4 0.6 . . . 2.0] H=[0,0.2,2.0]
The colon, :, is one of Matlab’s most important operators. 2. Operate with the vectors V1 = [1 2 3 4 5 6 7 8 9 0] V2 = [0.3 1.2 0.5 2.1 0.1 0.4 3.6 4.2 1.7 0.9] V3 = [4 4 4 4 3 3 2 2 2 1] (a) Calculate, respectively, the sum of all the elements in vectors V1, V2, and V3.
sum(V1)=45
sum(V2)=15
sum(V3)=29
(b) How to get the value of the fifth element of each vector? V1(5)=5
V2(5)=0.1
V3(5)=3
What happens if we execute the command V1(0) and V1(11)? Remember if a vector has N elements, their subscripts are from 1 to N. 1
(c) Generate a new vector V4 from V2, which is composed of the first five elements of V2. V4=V2(1:5)
Generate a new vector V5 from V2, which is composed of the last five elements of V2.
V5=V2(1,5:end)
(d) Derive a new vector V6 from V2, with its 6th element omitted. V6=V2
V6(6)=[]
Derive a new vector V7 from V2, with its 7th element changed to 1.4.
V7=V2
V7(7)=[1.4]
Derive a new vector V8 from V2, whose elements are the 1st, 3rd, 5th, 7th, and 9th elements of V2 V8=[V2(1,1) V2(1,3) V2(1,5) V2(1,7) V2(1,9)]
(e) What are the results of 9-V1
9-V1= [8 7 6 5 4 3 2 1 0 9]
V1*5 V1*5= [ 5 10 15 20 25 30 35 40 45 0]
V1+V2 V1+V2= [1.3 3.2 3.5 6.1 5.1 6.4 10.6 12.2 10.7 0.9]
V1-V3 V1.*V2 V1*V2 V1.^2 V1.^V3 V1^V3 V1 == V3 V1>6 V1>V3 V3-(V1>2) (V1>2) & (V1<6) (V1>2) | (V1<6) any(V1) all(V1) 2
3. Using Matlab help system, click on Help -> MATLAB help or type helpdesk to can open the help files. For description of a single function or command, type help command_name on the command line, or use ’search’ in the help window. For example, type help plot on the command line.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
4. Flow control What are the results of these sets of commands? Think them over and run them with Matlab to
see if you are right. (a) A = zeros(1,5); for n = 1:4 for m = 1:3 A = A + n*m; end end A (b) B = [1 0]; if (all(B)) B = B + 1; elseif (any(B)) B = B + 2; else B = B + 3; end B 3
(c) C = 7:2:22 num = 0; while (all( C>0)) C = C - 3; num = num + 1; end C num (b) Situations under which loops can be avoided. Does the set of commands: for i = 1:20 H(i) = i * 5; end have the same result as:
H = 1:20; H = H*5; Does the set of commands: for n = 1:100000 x(n) = sin(n*pi/10); end have the same result as: n = 1:100000; x = sin(n*pi/10); 5. Compare a script and a function (a) Write a script: In the main menu of Matlab, select file -> new -> M-file A new window will pop up. Input the following commands: 4
x = 1:5; y = 6:10; g = x+y; and then save the file as myscript.m under the default path matlab/work (b) Write a function: Create a new m file following the procedure of above. Type in the commands: function g = myfunction(x,y) g = x + y; and then save it as myfunction.m (c) Compare their usage (1) run the commands one by one: myscript x y
g z = myscript (error?) (2) Run command clear to remove all variables from memory (3) Run the commands one by one: x = 1:5; y = 6:10; myfunction (error?) z = myfunction(x,y) g (error?) a = 1:10; b = 2:11; myfunction(a,b) Can you explain what is going on here? 5
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help