You are on page 1of 13

NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.

E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Exercise 5-1
Problem 5-1.1
Write a script that converts from Polar to Cartesian coordinate system of point P(r,theta) to P(x,y).

Problem 5-1.2
Write a script that display the overlapping plots of sin(x), cos (2x), and sin(x)-cos (2(x). The sine
function is blue continuous curve, the cosine function is red dash curve, and the difference of the two
functions is black dotted curve. Horizontal label is “x” and vertical label is “f(x)”. Display the legend
identifying the curves with gridlines turned on. Set the title as “Plot of functions vs x”. X values is from the
range of -2*pi to 2*pi containing 100 data points.
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Problem 5-1.3
Write a script that describes the path of a projectile utilizing the x and y-axis as the reference
coordinates. Using the given set of data and formulas shown below, derive the equation of y as a function
of x, (y=f(x)). Use this equation to plot the path of the projectile, for the given range of x. Calculate the
value of y at x = 40m, and plot a point at this position using a red “*“ marker. Use dash (“--”) black line to
plot the curve.

Given:

Formulas:
[ ( )]
[ ( )]

List of possible Octave functions to use:


1. tand, secd, clf, hold, axis
2. find, printf, plot, grid
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Problem 5-1.4
Write a script that describe the plots of four (4) functions using different plot colors in one graph
subdivided into two (2) rows and two (2) columns as shown below, where t is a row vector containing
100 items from 0 to 2*pi.
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Exercises 5-2
Problem 5-2.1
Rewrite the part of the quadratic equation program above to handle solution to imaginary roots.
Test your program with the following values: a = 1, b = 1, and c =1. Check the answer as 0.5±0.86603i. Use
Octave’s escape sequence character code to display the “±” character.
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Problem 5-2.2
The income tax table shown below under the TRAIN Law is obtained from the internal revenue agency
and is used to compute the tax owed by an employee to the government. Develop a script that
implements this tax table.
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Exercise 5-3
Problem 5-3.1
The following table gives the approximate values of the static coefficient of friction u for various
materials.

To start a weight W moving on a horizontal surface, you must push with a force F, where F = μW. Write an
Octave program that uses the switch structure to compute the force F. The program should accept the
value of W and the type of materials as input and compute the corresponding force needed to just start
object moving.
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Problem 5-3.2
The area of a circle, triangle, and trapezoid is are be calculated one at a time based from the user
choice. Write a menu driven program which allows the user to select the figure he/she wants to the area
be calculated, depending on he selected figure, the user is ask to enter required data, the program
calculates the area, display the result on screen, and then exits the program. The test run is shown below.
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Problem 5-3.2
Curves are to be plotted one at a time on the polar coordinate with the following definitions:
 Angle theta is a set of values from 0 to 2.1*pi with an increment of 0.1.
Circle: ( ( )) where:
Spiral: ( )
Heart: ( ( ))
Rose: ( )
 The program is to display a menu containing the name of the curves defined above and a Quit item
to quit the program.
 The user is to press the first letter of the menu item to display the selected curve.
 The program is to ignore the requested item if the user press a wrong letter.
 Use the following color for each curve: Circle-green, Spiral – blue, Heart – red, and Rose – magenta.
Set the line width to 4.
 Use the Octave polar function to plot the curves, clearing the previous plot before displaying the
next selected curve and close it when the quit item is selected. Hint: Use the do-until and switch-
case control structure.

Exercises 5-4
Problem 5-4.1
Use a while loop to determine how many terms in the series 3k2, k =1, 2, 3, . . . , are required for the sum
of the terms to exceed 2000. What is the sum for this number of terms?
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Problem 5-4.2
A projectile is launched at an angle, theta from the horizontal with an initial velocity of vo. Write a script
that plot the trajectory of the projectile up to or just before it reaches the elevation from which it was
launched. Use the input command to enter the launch angle and velocity. User is to continuously be asked
to enter the launch angle and velocity if the entered value is less than zero or greater than 90 degrees, and
is less than zero or greater than 150m/s, respectively.

Problem 5-4.3
Convert the algorithm shown below to Octave script, where M and N are Octave’s variables:
1. Set M = 44 and N = 28
2. While M not equal to N repeat:
While M > N repeat:
Replace value of M by M− N
While N > M repeat:
Replace value of N by N − M
3. Display M
4. Stop.
a) Work through the structure plan, sketching the contents of M and N during execution. Give the
output.
b) Repeat (a) for M = 14 and N = 24.
c) What general arithmetic procedure does the algorithm carry out (try more values of M and N if
necessary)?
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Problem 5-4.4
An Armstrong number is a number which equal to the sum of the cubes of its individual digits. For
example, 153 is an Armstrong number as –
( ) ( ) ( )

Convert the algorithm shown below into an Octave program to print the Armstrong number from 1 to
500. Hint: You may use mod and idivide functions.

Problem 5-4.5
Develop a script that displays a dialog window that allows the user to select the figure to be drawn on
screen as shown below. Selecting the Circle option and then clicking the OK button should draw a circle of
red color and double line. Selecting the Square should draw a dashed square. The Quit option exits the
program. Use Octave menu command to display the menu window, you may also use the while and if-else
programming structures.
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Exercises 5-5
Problem 5-5.1
A set of assorted integers (positive and negative) are to be read in from the keyboard and stored in a row
vector variable. All positive integers (including zero) and all negative integers are to be counted
separately. Develop a script to count the number positive and negative numbers entered separately, and
then display the content and counts before the scrip exits. Use the printf function to display the required
results. Sample test run is shown on the adjacent side.

Problem 5-5.2
Suppose you are entering sales amounts into the computer in order to calculate extended totals (quantity
times the price per unit). You want the computer to display the quantity sold, name of the item, part
number, and extended total. Write a script that does just that until the entered part number is -999.
Pattern your program to the sample program run below.

Sample program:
NAME: JOHN NICHOL N. GUERRA SUBJECT: COMPUTER APPLICATIONS FOR M.E
SECTION: BSME-3C INSTRUCTOR: ENGR. CAYETANO RICAFRENTE

Problem 5-5.3
Add all terms in the series until the sum exceeds 1.995. Print out the sum, the number
of terms needed just to exceed the sum of 1.995, and the denominator of the last fraction. Sample run of
the program is shown below.

Exercises 5-6
Problem 5-6.1
The Fibonacci series is given as: 1 1 2 3 5 8 13 21 34 55 … Write an Octave program to generate and
print the Fibonacci series up to the15th term.

Problem 5-6.2
Write an Octave program to create a multiplication table from 1 to 100.

You might also like