CS10-8L: Computer Programming Laboratory Exercise #2: Variables, Scripts and Visualizations
CS10-8L: Computer Programming Laboratory Exercise #2: Variables, Scripts and Visualizations
OBJECTIVES
To introduce the usage of arrays in Matlab.
To enable the students to create simple files.
INSTRUCTIONS
A. Usual Functions in a Scientific Calculator
1. To find √ π , type sqrt(pi).
2. Trigonometric functions like sin(x) expect the argument x to be in radians. To address this, multiply degrees by
π / 180 to get radians. For example, getting the sine of 90 ° , you type:
sin(90*pi/180)
3. The exponential function e x is computed in Matlab as exp(x).
4. Note that names of user-defined variables should not duplicate any of the built-in functions. Try the following:
>> pi = 4 <Enter>
>> sqrt(pi) <Enter>
>> whos <Enter>
>> clear pi <Enter>
>> whos <Enter>
>> sqrt(pi) <Enter>
>> clear <Enter>
>> whos <Enter>
>> x = 0 : 10 <Enter>
>> a = [ 1 2 3, 4, 5, 6] <Enter>
>> size(x) <Enter>
2. Arrays can now be defined (or created) in terms of the vector x just defined. Try
>> y = 2 .* x <Enter>
>> w = y ./ x <Enter>
>> z = sin(x) <Enter>
>> b = magic(10) <Enter>
3. You can use one vector in a list for another one. Try:
x+ 2 y =4
2 x− y=3
Two approaches can be done:
Approach 1: Matrix method
D. Other Functions
3. Save the file in the current directory as sample.m in the current directory.
4. In Command Window, you may run the saved script file by typing:
Note: A script file may be listed in the Command Window with the command type, e.g.
5. When you run a script, you have to make sure that Matlab’s current directory is set to the directory to which the
script is saved. To change directory, browse through the current directory or change from the command line, e.g.
F. Capturing output
1. To capture output in Command Window, type
MACHINE PROBLEM
Answer the following problems using commands in Matlab. Make sure to indicate your commands in your
submission.
1. Solve the following system of linear equations using the Matrix Method:
−2 x+ y−6 z=11
−4 x− y −3 z =1
8 x − y+ 4 z=−1
2. Create the following vectors using the indicated operators:
Colon operator:
a. 3 4 5 6
b. 1.0000 1.5000 2.0000 2.5000 3.0000
c. 5 4 3 2
linspace function:
d. 4 6 8
e. -3 -6 -9 -12 -15
3. The function sin calculates and returns the sine of an angle in radians. Use help elfun to find the name of the
function that returns the sine of an angle in degrees. Verify that calling this function and passing 90 degrees to it
results in 1.
4. Create scripts for the following problems. Make sure to save the file using the indicated name.
a.) weight.m
Create a variable, pounds, to store the weight in pounds, say 165 lbs. Convert this to kilograms and assign the
result to a variable kilos. The conversion factor is 1 kg = 2.2 pounds.
b.) resistance.m
The combined resistance RT of three resistors R1, R2, and R3 in parallel is given by
1
RT =
1 1 1
+ +
R1 R 2 R 3
Create variables for the three resistors and store the values 3, 4, 5 in each, and then calculate the combined
resistance.
c.) vectorcoordinates.m
A vector can be represented by its rectangular coordinates x and y or by its polar coordinates r and θ . The
relationship between them is given by the equations
x=r∗cos θ
y=r∗sin θ
Assign values for the polar coordinates to variables r = 2 and theta = 45 degrees. Then, using these values, assign
the corresponding rectangular coordinates to variables x and y.
ANSWERS
1.
Result: 1, 1, -2
Commands:
a = [-2 1 -6; -4 -1 -3; 8 -1 4]
a =
-2 1 -6
-4 -1 -3
8 -1 4
b =[ 11; 1; -1]
b =
11
1
-1
x = linsolve (a,b)
x =
1
1
-2
2. Commands:
a. a = 3:6
b. b = 1:0.5:3
c. c = 5:-1:2
d. d= linspace(4, 8, 3)
e. e= linspace(-3, -15, 5)
3.
Function name: sind(90)
Commands:
ans =
4.
a. weight.m
Weight in kilos: 74.8427kg
Script:
convmass ([165], 'lbm' , 'kg')
ans =
74.8427
b. resistance.m
Combined resistance: 1.2766 ohms
Script:
R_1=3;
R_2=4;
R_3=5;
R_T=1/(1/R_1+1/R_2+1/R_3)
R_T =
1.2766
c. vertor coordinates.m
x: 1.0506
y: 1.7018
Script:
r=2;
theta=45;
x=r * cos(theta)
y=r * sin(theta)