AnshumSoodHw.3
Contents
Problem
Problem
Problem
Problem
Problem
Problem
Problem
Problem
Problem
Problem
Problem
Problem
Problem
1
2
3
4
5
6
7
8
9
10
11
12
13
Problem 1
%(a)
type Day2Num
Day2Num('tUesDay')
function num=Day2Num(day)
switch lower(day)
case '
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2013
Tips and tricks for lab 3
E7 lab section (number 12)
University of California at Berkeley
February 8, 2016
What is branching?
We as humans often make decisions based on certain conditions.
For example:
When I go to school in the morning, if it is raining
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2013
1. What will the value of R be after the following lines are executed?
1 > Q = true;
2 > P = 1 < 0;
3 > R = (Q   P) & Q;
(a) Matlab will have an error on line 1
(c) Matlab will have an error on line 2
(d) *true
(e) Matlab will have an error on line 3
n
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2014
Lecture 5: Function Examples, Scripts, Publishing
Calculating with Scalars and Vectors
Suppose S = 2 and V = [ 1 4 3 ]
1. Calculating with Scalars and Vectors
2. Function Examples
a) calculate norm of a vector
b) find the roots of a quadratic
c) create ch
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2014
Lecture 4: Functions
1.
2.
3.
4.
5.
6.
7.
What are functions?
What are functions?
Anonymous functions
Function plotting revisited
Creating custom functions (using mfiles)
Scope of variables
Functions with multiple outputs
Subfunctions
Much of MATLAB's p
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2014
Lecture 6: Branching
Branching using if
Branching means that MATLAB will choose whether to
execute a block of code depending on a test condition
if TestCondition
command1;
command2;
These commands are carried out by
MATLAB only if TestCondition is true
co
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2014
Lecture 7: Iteration
for loops
1. for loops
2. while loops
3. interrupting normal program flow
Most common form of a for loop:
Computers are good at doing repetitive calculations.
% MATLAB iterates (cycles through the loop) multiple times.
% value of k st
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2014
Lecture 2 Using MATLAB as a Calculator
MATLAB Command Window
1. MATLAB command window
2. Using MATLAB as a calculator
a. Basic math
b. Builtin functions (e.g., trig, logarithms)
c. Infinity (Inf) and Not a Number (NaN)
d. Displaying and storing results o
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2014
Lecture 8: Recursion
Factorial Function
Recursive functions are functions that call themselves.
Recursive functions are functions that call themselves.
Using recursion can sometimes be simpler than iteration
when writing custom functions in MATLAB.
Consid
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2014
Lecture 9: Time Complexity
How to Rate a Function?
1) Does the function give correct outputs for
all (or at least most) possible inputs?
2) Does the function handle errors gracefully?
3) How much time is needed to do the job?
4) How much space (memory, di
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2014
Welcome to E7
Introduction to Computer Programming
For Scientists and Engineers
Lecture 1 Introduction
Topics to be covered today
Instructor, GSIs, enrollment
What is E7 about? Why MATLAB?
MATLAB Software and Textbooks
Lectures, labs, and discussion
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2013
Notes (not a lecture!) about plotting with Matlab
E7 lab section (number 12)
University of California at Berkeley
February 29, 2016
Plot examples
Not a great plot:
A better plot:
2 / 20
The good plot (nonexhaustive) checklist
I
Is each written element ea
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2013
Notes (not a lecture!) about arrays and indexing
E7 lab section (number 12)
University of California at Berkeley
January 27, 2016
Defining 2D arrays
Use square brackets to define arrays
I Use a comma (or just a space) to list values of a row.
I Use a sem
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2013
Notes (not a lecture!) about functions
E7 lab section (number 12)
University of California at Berkeley
February 1, 2016
Elements of a function definition
A Matlab function performs a task when it is called (e.g., calculate
something). When calling a funct
Anshum Sood Hw5
Contents
Problem 1
Problem 2
Problem 3
Problem 4
Problem 5
Problem 6
Problem 7
Problem 8
Problem 9
Problem 10
Problem 11
Problem 12
Problem 13
Problem 14
Problem 15
Problem 1
fixedcost=2045000
Q=0:10000000;
R=(6*10^6)Q)/(1.1*10^6).*Q;
var
Anshum Sood Hw5
Contents
Problem 1
Problem 2
Problem 3
Problem 4
Problem 5
Problem 6
Problem 7
Problem 8
Problem 9
Problem 10
Problem 11
Problem 12
Problem 13
Problem 14
Problem 15
Problem 1
fixedcost=2045000
Q=0:10000000;
R=(6*10^6)Q)/(1.1*10^6).*Q;
var
AnshumSoodHw4
Contents
Problem
Problem
Problem
Problem
Problem
Problem 1
%(a)
for a=1:10
a^2
end
ans =
1
ans =
4
ans =
9
ans =
16
ans =
25
ans =
36
1
2
3
4
5
36
ans =
49
ans =
64
ans =
81
ans =
100
%(b)
for b=20:1:13
log(b)
end
ans =
2.9957
ans =
2.9444
Anshum Sood Hw.2
Contents
problem 1
1b
Problem 2
Problem 2a
Problem 2b
problem 2c
Problem 2d
problem 2e
problem 3
problem 3a
problem 3b
problem 3c
problem 3d
problem 3e
problem 3f
problem 4
problem 4a
4b
4c
4d
4e
4f
4g
4h
5
5b
5c
6
7
7a
7b
7c
8
8a
8b
8c
8
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2013
Department of Mechanical Engineering
Instructors: M. Frenklach, R. Horowitz
University of California, Berkeley
Fall Semester 2012
E7 Midterm Examination 1
October 12, 2012
NAME : SOLUTION
SID :
SECTION :
LAB :
1 or 2 (please circle your discussion section
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2013
Department of Mechanical Engineering
Instructors: M. Frenklach, P. Papadopoulos
University of California, Berkeley
Fall Semester 2010
E7 Midterm Examination 1
October 1st, 2010
NAME :
SID :
SECTION :
LAB :
1 or 2 (please circle your lecture section)
#11:
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2013
UNIVERSITY OF CALIFORNIA BERKELEY
Department of Civil and Environmental Engineering
Spring 2013
Engineering 7
Professor: S. Govindjee
E7
Midterm Exam
1:10 2:00pm; March 13, 2013
n
Closed Everything
You may only have out a #2 pencil, eraser, and your scant
Introduction toComputer Programming for Scientists and Engineers.
ENG 7

Spring 2013
E7 Midterm Exam
Introduction to Computer Programming for Scientists and Engineers
Spring Semester 2012
University of California at Berkeley
NAME:
_
SID:
_
Lab Group: _
Read these instruc