Affan Telek - F23 - Lab06 - Programming
Affan Telek - F23 - Lab06 - Programming
Sometimes, advanced plots are necessary to illustrate results. Plots like bar graphs,
pie charts, histograms, polar plots, 3D plots as well as plots on non-linear axes like
logarithmic present data in a different fashion more suitable for drawing
conclusions.
In many situations, the sequence of steps that are prescribed in a program depends
on given inputs. For example, a (sub-)set of steps may be skipped, or a different
(sub-)set of steps can be executed depending on the input values. Matlab offers
different tools to control the flow of a program.
The goal of this lab is to allow you the opportunity to try some of the Matlab
capabilities in producing specialised 2D and 3D plots and to allow you the
opportunity to write scripts for some given problems using the selection and
repetition control structures.
Reading and related topics: Lecture slides (Lessons #6 and #7) and chapters 5, 6
and 10 of Amos Gilat’s book.
Use Matlab to solve the following five problems. Save the code you wrote to solve
them, together with the result of it in a report. Make sure you include enough
comments in your code. Save your report in PDF or MLX format.
Problem 1:
Make a polar plot of the function r = ±√θ for 0 ≤ θ ≤ 5π. It is known as a Fermat
spiral and it looks like the figure below with the labels.
CPS118 > Lab 05 > Plotting in MATLAB 3
Problem 2:
Molecules of a gas in a container are moving around at different speeds. Maxwell’s
speed distribution law gives the probability distribution P(ν) as a function of
temperature and speed:
where M is the molar mass of the gas in kg/mol, R = 8.31J/(mol K), is the gas
constant, T is the temperature in kelvins, and ν is the molecule’s speed in m/s.
Make a 3-D plot of P(ν) as a function of ν for 0 ≤ ν ≤ 1000 m/s and 70 ≤ T ≤ 320K
for oxygen (molar mass 0.032 kg/mpl). Your plot must have labels.
CPS118 > Lab 05 > Plotting in MATLAB 4
Problem 3:
Write a program that asks the user to input a vector of integers of arbitrary length.
Then, using a for-end loop the program examines each element of the vector. If
the element is positive, its value is doubled. If the element is negative, its value is
tripled. The program displays the original vector that was entered and the
modified vector.
Problem 4:
The Pascal triangle is a triangular array of the binomial coefficients. To build the
Pascal triangle, start with number 1 at the top, then continue placing numbers
below it in a triangular pattern, with the second row having two numbers, the
third row having 3 numbers, and so on. Each row starts and ends with number 1,
with all the other numbers being the sum of the two numbers directly above them
(See figure below):
CPS118 > Lab 05 > Plotting in MATLAB 5
Write a MATLAB program that creates this n × n matrix that displays n rows of
Pascal’s triangle. Use the program to create 6 and 9 rows of Pascal's triangles. (One
way to calculate the value of the elements in the lower portion of the matrix is:
CPS118 > Lab 05 > Plotting in MATLAB 6
Problem 5:
The overall grade in a course is determined from the grades of 10 quizzes, 1
midterm, and a final exam, using the following scheme:
Quizzes: Quizzes are graded on a scale from 0 to 10. The grade of the two lowest
assignments is dropped and the average of the eight assignments with the higher
grades constitutes 25% of the course grade.
Midterm and final exams: Midterm and final exams are graded on a scale from 0 to
100. If the midterm grade is higher than, or the same as, the grade on the final
exam, the midterms constitute 35% of the course grade and the grade of the final
exam constitutes 40% of the course grade. If the final exam grade is higher than the
midterm grade, the midterm constitutes 25% of the course grade and the grade of
the final exam constitutes 50% of the course grade.
(a) Quiz grades: 9.5, 6, 4, 10, 7.8, 3.4, 9, 5.6, 9, 10, midterm grade: 73, and final
exam: 84.
(b) Quiz grades: 9.5, 8.4, 9, 10, 7.8, 10, 9, 9.6, 9, 10, midterm grade: 89, and final
exam: 81.
CPS118 > Lab 05 > Plotting in MATLAB 7
This lab is to be done individually. You need to submit a lab report with the
solutions to the five problems, and the report submitted should represent your
work. Your lab report must be in PDF or MLX format (create one section for
each problem). Submit your lab report on D2L/Brightspace before November
15, 2023 at 11:59 PM. Late submissions will not be accepted.
Have fun!
CPS118 > Lab 05 > Plotting in MATLAB 8