CSC 113, Fall 2010
Lab Assignment 6
This assignment develops an animation in which a ball drops due to gravity, and (with some
further work) bounces back again.
First we need to set up a canvas and a bounding box, which can be achieved using the following
CS113 Introduction to Computing
for Engineers
MATLAB: Script and Function Files
1
MATLAB Script Files
A MATLAB script file (Called an M-file) is a text (plain
ASCII) file that contains one or more MATLAB
commands and, optionally, comments.
The file is s
CS113 Introduction to Computing
for Engineers
Logical Operators and Conditional
Statements
1
Programming in Matlab
2
Programming in Matlab
3
Programming in Matlab
4
Programming in Matlab
5
Programming in Matlab
6
Programming in Matlab
7
Programming in Mat
CS113 Introduction to Computing
for Engineers
Iteration
1
REVIEW FROM LAST LECTURE
2
REVIEW FROM LAST LECTURE
3
REVIEW FROM LAST LECTURE
4
REVIEW FROM LAST LECTURE
5
REVIEW FROM LAST LECTURE
6
CONDITIONAL STATEMENTS APPLIED TO
MATRICES
> A = [ 0.5, 6.0, -
CSC 113
Lecture 9
Strings
Strings
A s tr i n g i s a n a r r a y o f c h a r a c te r s
s = 'abc'
is equivalent to s = [ 'a' 'b' 'c' ]
A l l o p e r a ti o n s th a t a p p l y to v e c to r s a n d
a r r a y s c a n b e u s e d to g e th e r w i th s
The switch Statement
THE switch STATEMENT:
Provides a convenient way to execute
conditional code when there are many
cases to choose from.
This construct can replace series of ifelse-end statements
LOGICAL PROGRAMMING
CONSTRUCTS
SYNTAX:
switch expressio
CS113
Introduction to Computing
for Engineers
Curve Fitting and Approximation
APPROXIMATION
OF DATA
Least Squares Method of Curve Fitting
Fitting Data to a Curve: Determining a physical
parameter (e.g. spring constant)
x
Data points (xi, Fi) can be appro
EXAMPLE PROJECT
HANGMAN
The goal of this project was to write a graphically based game of hangman using Matlab.
The computer program opens a figure window, sets the axes properly and then
draws the "hangman" gallows.
The computer loads a dictionary of w
CS 113
Linear Systems of Equations
Linear Algebraic Equations
Nonlinear Equations: Example
Representing Linear Algebraic Equations
in Matrix Form
Ax = b
Representing Linear Algebraic Equations
in Matrix Form
Ax = b
a11
a21
A=
M
an1
a12
a22
M
an 2
L a1n
C SC 113
M id t e r m P r o j e c t : M a t h P r a c t ic e P r o g r a m
A s s ig n e d : O c to b e r 1 5 , 2 0 1 0
D u e : O c to b e r 2 9 , 2 0 1 0
O b je c tiv e :
T o te s t y o u r k n o w le d g e o f lo o p s , s trin g s , c o n d itio n a l s
MATLAB Graphics 101
CSC 113
Objects
~There are several different types of object in
MATLAB, but the ones were primarily
concerned with will text objects and patch
or fill objects.
~An object is graphical image created within
the axes of a figure window.
F
Starter
Yahtzee prototype
Project Outlines:
Yahtzee and Checkers
CSC 113
Fall 2010
Checkers prototype
Starter
Yahtzee prototype
Checkers prototype
Gaphic user interfaces can be built a couple of ways in Matlab:
Manual coding;
Using GUIDE (Graphic User Int
CSC 113: Saving Plots and Graphics in Matlab
This document covers the basics of saving a gure created in M ATLAB, so that it may be used in
some other setting, such as a web page, written report, or overhead presentation.
First, lets create a gure. The fo
Symbolic Computations
CSC 113
Fall 2010
Conventional Computations
Matlab was designed to execute serial computations:
value = expression;
For example, suppose we have
D = D0 eQ/RT
If we know the values on the right-hand side, we can calculate D:
>
>
>
>
>
CS113 Introduction to Computing
for Engineers
Plots and Graphs I
1
Background
While numerical methods are the heart (and origin) of
Matlab, graphics has become the major component since
the release of Version 4.
Version 6 adds to this legacy with refine
Rules About Variables
15
Rules About Variables
16
Predefined Variables
17
Some Useful Commands
18
The ; and % operators
19
Big deal, a calculators $20
Matlab is a fully-functional programming language
This means we get variables
name = value
Name can
CSC 113
Midterm Project: Class Average
Assigned: October 15, 2010
Due: October 29, 2010
Objective:
To test your knowledge of loops, strings, matrix references, and processing data in
matrices as well as introducing the concepts of cell arrays and reading
C SC 113
H om ew ork 5
Assigned: November 12, 2010
Due: November 19, 2010
Directions: Write ONE script file that solves all of the following problems. Only the answers
should be output to the command window.
1) Plot the following data (using markers, i.e.
CSC 113
Homework Assignment 1
Assigned September 13, 2010;
Due September 20, 2010.
1. You are constructing a hemispherical dome with an outer radius of 50 feet. The walls will
be solid concrete 8 inches thick. How many cubic yards of concrete will be need
CSC 113
Homework Assignment 2
PLOTS!
Assigned September 20, 2010;
Due September 27, 2010.
1. Basic function plots:
(a) Plot the function f (x) = ex over the interval 0 x 10. (Hint: in MATLAB, ex is
expressed as exp(-x).)
(b) Plot the function f (x) = ex c
CSC 113
Homework Assignment 3
Assigned September 27, 2010;
Due October 4, 2010.
1. Write a MATLAB function le that computes a plots 5 cycles of a sine wave as a function
of time:
y(t ) = A sin(2 f t + ).
The inputs to this function should be: (i) the ampl
C SC 113
H om ew ork 4
Assigned: October 4, 2010
Due: October 12, 2010
1) The purpose of this assignment is to make use of newly learned conditional statements
and for loops in analyzing a matrix of integers. Two text files will be available on the
class
CSC 113
Lab Assignment 3
1. Write a MATLAB script le that computes and plots 5 cycles of a sine wave as a function
of time.
Assume the sine wave has a frequency of f = 100 Hz, an amplitude of A = 0.9, and a
phase angle of = 45 degrees. The equation of the
CSC 113, Fall 2010
Lab Assignment 4
1. Write a M ATLAB function le that computes the factorial of a positive integer. The factorial
of an integer N is
N ! = N (N 1) (N 2) 2 1
For example, 5! = 5 4 3 2 1 = 120 and, by convention, 0! = 1.
Your program shoul
CSC 113, Fall 2010
Lab Assignment 5
1. Write a M ATLAB function le that that sorts a vector of numbers from the smallest to the
largest, without using the M ATLAB sort command.
The input to the program should be a vector containing the values to sort, and
CSC 113, Fall 2010
Lab Assignment 7
All example functions or scripts below should be created in your editor and saved to your workspace,
so that you may run them.
1. Consider the following M ATLAB function which accepts a two-element vector, and plots it
CSC 113, Fall 2010
Lab Assignment 8
The various commands below are to be typed into the command window; observe their outputs.
1. Projectile trajectory! From basic physics, the x and y displacements of a projectile red at
an initial velocity v0 with an an