Skip to content

Update #2

Merged
merged 17 commits into from Feb 15, 2017
Merged
7 changes: 5 additions & 2 deletions HW2/projectile.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions HW3/README.md
@@ -0,0 +1,73 @@
# Homework #3
## due 2/15/17 by 11:59pm


1. Create a new github repository called 'roots_and_optimization'.

a. Add rcc02007 and pez16103 as collaborators.

b. Clone the repository to your computer.

c. Copy your `projectile.m` function into the 'roots_and_optimization' folder.
*Disable the plotting routine for the solvers*

d. Use the four solvers `falsepos.m`, `bisect.m`, `newtraph.m` and `mod_secant.m`
to solve for the angle needed to reach h=1.72 m, with an initial speed of 15 m/s.

e. The `newtraph.m` function needs a derivative, calculate the derivative of your
function with respect to theta, `dprojectile_dtheta.m`. This function should
output d(h)/d(theta).


f. In your `README.md` file, document the following under the heading `#
Homework #3`:

i. Compare the number of iterations that each function needed to reach an
accuracy of 0.00001%. Include a table in your README.md with:

```
| solver | initial guess(es) | ea | number of iterations|
| --- | --- | --- | --- |
|falsepos | | | |
|bisect | | | |
|newtraph | | | |
|mod_secant | | | |
```

ii. Compare the convergence of the 4 methods. Plot the approximate error vs the
number of iterations that the solver has calculated. Save the plot as
`convergence.png` and display the plot in your `README.md` with:

`![Plot of convergence for four numerical solvers.](convergence.png)`

iii. In the `README.md` provide a description of the files used to create the
table and the convergence plot.

2. The Newton-Raphson method and the modified secant method do not always converge to a
solution. One simple example is the function f(x) = x*exp(-x^2). The root is at 0, but
using the numerical solvers, `newtraph.m` and `mod_secant.m`, there are certain initial
guesses that do not converge.

a. Calculate the first 5 iterations for the Newton-Raphson method with an initial
guess of x_i=2 for f(x)=x*exp(-x^2).

b. Add the results to a table in the `README.md` with:

```
### divergence of Newton-Raphson method

| iteration | x_i | approx error |
| --- | --- | --- |
| 0 | 2 | n/a |
| 1 | | |
| 2 | | |
| 3 | | |
| 4 | | |
| 5 | | |
```

c. Repeat steps a-b for an initial guess of 0.2. (But change the heading from
'divergence' to 'convergence')

3. Commit your changes to your repository. Sync your local repository with github. Then
copy and paste the "clone URL" into the following Google Form [Homework 3](https://goo.gl/forms/UJBGwp0fQcSxImkq2)
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -74,8 +74,8 @@ general, I will not post homework solutions.
|3|1/31||Consistent Coding habits|
| |2/2|5|Root Finding|
|4|2/7|6|Root Finding con’d|
| |2/9|7|Optimization|
|5|2/14||Intro to Linear Algebra|
| |2/9|7| **Snow Day**|
|5|2/14|| Optimization |
| |2/16|8|Linear Algebra|
|6|2/21|9|Linear systems: Gauss elimination|
| |2/23|10|Linear Systems: LU factorization|
Expand Down
Binary file added lecture_05/gp_image_01.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
389 changes: 370 additions & 19 deletions lecture_05/lecture_05.ipynb

Large diffs are not rendered by default.

245 changes: 230 additions & 15 deletions lecture_05/lecture_05.md
@@ -1,4 +1,193 @@


```octave
%plot --format svg
```

## Questions from last class

When you execute the given function

my_function.m:

```matlab
function [x,y] = my_function(max_time)
N=100;
t=linspace(0,max_time,N);
x=t.^2;
y=2*t;
end
```

as

```>> [x,y] = my_function(20);```

What variables are saved to your workspace?

![responses](q1.png)

How do you write a help description for a function?

![responses to question 2](q2.png)


How to keep our forked ME3255S page up to date with the original
pretty tired this morning

How do I use the Github Desktop?

whats your favorite football team?

Will UConn's github get updated to the newest version of github?
As u said in class trail and error is the best way of learning.

I believe the % is the same as matlab where it de-links your code into text

Does the @ symbol designate a pointer?

Given the change of air pressure as altitude increases, how fast would a frisbee have to travel (and spin) to hit an airplane?

What is a gui?


could you go over a nested for loop example

Can't seem to get this function to produce any graph and am not sure why

When are these google forms due?

how do I create a new function using Github on my desktop?

Can you explain the first question more in class?

What is the meaning of life?

Should I just know how or what these topics are or will we learn them in the future?



```octave
f =@(x) x.^2
```

f =

@(x)x.^2



```octave
f([1:2:10])
f(4)
```

ans =

1 9 25 49 81


ans =

16



```octave
% nested for loop example
for i = [1:6]
for j = [1:3]
fprintf('i=%i and j=%i\n',i,j)
end
end
```

i=1 and j=1
i=1 and j=2
i=1 and j=3
i=2 and j=1
i=2 and j=2
i=2 and j=3
i=3 and j=1
i=3 and j=2
i=3 and j=3
i=4 and j=1
i=4 and j=2
i=4 and j=3
i=5 and j=1
i=5 and j=2
i=5 and j=3
i=6 and j=1
i=6 and j=2
i=6 and j=3


# From last class


```octave
help my_function
```

Help documentation of "my_function"
This function computes the velocity in the x- and y-directions given
three vectors of position in x- and y-directions as a function of time
x = x-position
y = y-position
t = time
output
vx = velocity in x-direction
vy = velocity in y-direction



```octave
help my_caller
```

Help documentation of "my_caller"
This function computes the acceleration in the x- and y-directions given
three vectors of position in x- and y-directions as a function of time
x = x-position
y = y-position
t = time
output
ax = acceleration in x-direction
ay = acceleration in y-direction



```octave
t=linspace(0,10,100)';
x=t.^3; % vx = 3*t^2
y=t.^2/2; % vy = t
[vx,vy]=my_function(x,y,t);
[ax,ay]=my_caller(x,y,t);
yyaxis left
plot(t(1:10:end),ax(1:10:end),'o',t,6*t)
ylabel('a_{x}')
yyaxis right
plot(t(1:10:end),ay(1:10:end),'s',t, 1*t./t)
ylabel('a_{y}')
xlabel('time')
axis([0,10,0,3])
```


![png](lecture_05_files/lecture_05_11_0.png)



```octave
diff_match_dims(x,t)
```

Undefined function 'diff_match_dims' for input arguments of type 'double'.


# Good coding habits
## naming folders and files

Expand All @@ -24,23 +213,18 @@ function i=code(j)
end
```

Error: Function definitions are not permitted in this context.



```octave
help code
```

'code' is a command-line function

Example of bad variable names and bad function name

code not found.

Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.
Use the Help browser search field to search the documentation, or
type "help help" for help command options, such as help for methods.


## Choose variable names that describe the variable
Expand Down Expand Up @@ -85,8 +269,8 @@ help counting_function
1. Clone your homework_1 to your computer
2. open Matlab (cli, jupyter or gui)
3. Change working directory to homework_1 *e.g.* Windows:`cd('C:\Users\rcc02007\Documents\Github\homework_1')`, Mac: `cd('/Users/rcc02007/Documents/Github/homework_1')`
4. You have already created your first script `myscript.m` (if not see lecture_4)
5. Run `>> my_script.m`
4. You have already created your first script `setdefaults.m` (if not see lecture_4)
5. Run `>> setdefaults.m`
6. Create a new m-file called nitrogen_pressure.m
7. Create a function based upon the ideal gas law for nitrogen, Pv=RT
1. R=0.2968 kJ/(kg-K)
Expand All @@ -96,14 +280,45 @@ help counting_function
9. After file is 'committed', 'push' the changes to your github account

for the command-line git user, this is steps 8 and 9:

1. `$ git add *`
2. `$ git commit -m 'added file nitrogen_pressure.m'`
3. `$ git push -u origin master
Username for 'https://github.uconn.edu':rcc02007 <enter>
`
Password for 'https://rcc02007@github.uconn.edu': `


Now, use this function to plot the range of pressures that a pressure vessel would experience if it is 1000 gallons (3.79 m^3) with 10-20 kg of Nitrogen and temperatures range from -10 to 35 degrees C.

```matlab
v=0.379/linspace(50,20,10);
T=273.15+linspace(-10,35,10);
[v_grid,T_grid]=meshgrid(v,T);
P = nitrogen_pressure(v,T);
pcolor(v_grid,T_grid,P)
```


```octave
setdefaults;
v=3.79./linspace(10,20,10);
T=273.15+linspace(-10,35,10);
[v_grid,T_grid]=meshgrid(v,T);
P = nitrogen_pressure(v_grid,T_grid);
pcolor(v_grid,T_grid-273.15,P-100)
xlabel('specific volume (m^3/kg)')
ylabel('Temperature (C)')
%zlabel('Pressure (kPa)')
%colormap winter
%colormap summer
%colormap jet
colorbar()
```


![png](lecture_05_files/lecture_05_25_0.png)



```octave
Expand Down
Binary file modified lecture_05/lecture_05.pdf
Binary file not shown.
Binary file added lecture_05/lecture_05_files/lecture_05_11_0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lecture_05/lecture_05_files/lecture_05_25_0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.