0% found this document useful (0 votes)
63 views7 pages

CS10-8L: Computer Programming Laboratory Exercise #2: Variables, Scripts and Visualizations

This document provides instructions for an exercise in Matlab. It covers several topics: - Using arrays and defining vectors using operators like colon and linspace - Solving systems of linear equations using matrix methods or built-in functions - Creating and running script files - Capturing output in the command window It includes examples of commands to create and manipulate vectors, plot functions, solve sample problems, and save scripts for problems involving weight conversion, resistor combinations, and converting between rectangular and polar coordinates.

Uploaded by

BING BONG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views7 pages

CS10-8L: Computer Programming Laboratory Exercise #2: Variables, Scripts and Visualizations

This document provides instructions for an exercise in Matlab. It covers several topics: - Using arrays and defining vectors using operators like colon and linspace - Solving systems of linear equations using matrix methods or built-in functions - Creating and running script files - Capturing output in the command window It includes examples of commands to create and manipulate vectors, plot functions, solve sample problems, and save scripts for problems involving weight conversion, resistor combinations, and converting between rectangular and polar coordinates.

Uploaded by

BING BONG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CS10-8L: Computer Programming Laboratory

Exercise #2: Variables, Scripts and Visualizations

Name: Marcus Del Rosario Score:

Section: A34 Date: 08/31/2022

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>

B. Working with Arrays


1. Arrays hold the key in many powerful features of Matlab. Try the following:

>> 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:

>> a = [ 1 2 3]; <Enter>


>> b = [ 4 5]; <Enter>
>> c = [a –b] <Enter>
>> a = [a 0 -1] <Enter>

4. There are several ways to initialize a vector.

>> x = 1:10 <Enter>


>> x = 1:0.5:4 <Enter>
>> x = 10:-1:1 <Enter>
>> x = 1:2:6 <Enter>

Prepared by: Engr. Cheryl Mari M. Isip


CS10-8L: Computer Programming Laboratory
Exercise #2: Variables, Scripts and Visualizations

>> x = 0:-2:-5 <Enter>


>> x=linspace(0, pi/2, 10) <Enter>

5. To transpose a vector, we use an apostrophe(‘). Try:

>> x = 1:5 <Enter>


>> x’ <Enter>
>> y = [ 1 4 8 0 -1]’ <Enter>

6. To refer to particular elements of a vector by means of subscripts, try:

>> r = rand(1,7) <Enter>


>> r(3) <Enter>
>> r(2:4) <Enter>
>> r(1:2:7) <Enter>
>> r([1 7 2 6]) <Enter>
>> r([1 7 2]) = [ ] <Enter>
>> r = [ 1 2 3; 4 5 6; 7 8 9] <Enter>
>> r(3,2) <Enter>

7. To draw a reasonably nice graph of sin(x), enter the following commands:

>> x = 0 : 0.1 : 10; <Enter>


>> z = sin(x); <Enter>
>> plot(x,z), grid <Enter>
>> w = sin(2*x); <Enter>
>> plot(x,w), grid <Enter>
>> ezplot(‘tan(x)’) <Enter>

C. Solving systems of linear equations


1. If you are tasked to find the solution of two simultaneous equations, e.g.

x+ 2 y =4
2 x− y=3
Two approaches can be done:
Approach 1: Matrix method

>> a = [ 1 2; 2 -1]; <Enter>


>> b = [4; 3]; <Enter>
>> x = a\b <Enter>

Approach 2: Built-in solve function


>> [x y] = solve(‘x+2*y=4’, ‘2*x-y=3’) <Enter>
>> whos <Enter>
>> x = double(x), y = double(y) <Enter>
>> whos

D. Other Functions

1. Try the following commands:

Prepared by: Engr. Cheryl Mari M. Isip


CS10-8L: Computer Programming Laboratory
Exercise #2: Variables, Scripts and Visualizations

>> date <Enter>


>> calendar <Enter>
>> clc <Enter>
>> help <Enter>
>> help elfun <Enter>
>> lookfor eigenvalue <Enter>
>> demo <Enter>

2. Just for fun:

>> load handel <Enter>


>> sound(y,Fs) <Enter>
>> load laughter <Enter>
>> sound(y,Fs) <Enter>
>> load earth <Enter>
>> image(X); colormap(map) <Enter>
>> axis image <Enter>

>> why <Enter>


>> why(2) <Enter>
>> why(2) <Enter>

E. Working with Programs


1. If you want to solve problems which Matlab cannot do in one line, you will need to write programs. To access the
editor, you may choose any of the following:
a. Select File > New > M-file from Matlab desktop
b. Click the new file button on the desktop toolbar
c. Type edit then hit Enter in Command Window

2. Type the following into the Editor:

x = 0 : pi/20 : 6 * pi; <Enter>


plot(x, exp(-0.2*x) .*sin(x), ‘r’), grid <Enter>

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:

>> sample <Enter>

Note: A script file may be listed in the Command Window with the command type, e.g.

>> type sample <Enter>

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.

>> cd \mystuff <Enter>

F. Capturing output
1. To capture output in Command Window, type

Prepared by: Engr. Cheryl Mari M. Isip


CS10-8L: Computer Programming Laboratory
Exercise #2: Variables, Scripts and Visualizations

>> diary filename <Enter>


>> diary off <Enter>

Prepared by: Engr. Cheryl Mari M. Isip


CS10-8L: Computer Programming Laboratory
Exercise #2: Variables, Scripts and Visualizations

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.

Prepared by: Engr. Cheryl Mari M. Isip


CS10-8L: Computer Programming Laboratory
Exercise #2: Variables, Scripts and Visualizations

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:

>> help elfun


>> sind(90)

Prepared by: Engr. Cheryl Mari M. Isip


CS10-8L: Computer Programming Laboratory
Exercise #2: Variables, Scripts and Visualizations

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)

Prepared by: Engr. Cheryl Mari M. Isip

You might also like