Skip to content

Merge lecture 6. #4

Merged
merged 3 commits into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions HW2/projectile.svg
Loading
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/gp_image_01.png
Loading
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
Original file line number Diff line number Diff line change
@@ -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
Loading
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions lecture_05/my_caller.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function [ax,ay]=my_caller(x,y,t)
% 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

function v=diff_match_dims(x,t)
v=zeros(length(t),1);
v(1:end-1)=diff(x)./diff(t);
v(end)=v(end-1);
end

[vx,vy]=my_function(x,y,t);

ax = diff_match_dims(vx,t);
ay = diff_match_dims(vy,t);

end
21 changes: 21 additions & 0 deletions lecture_05/my_function.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function [vx,vy] = my_function(x,y,t)
% 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

vx=zeros(length(t),1);
vy=zeros(length(t),1);

vx(1:end-1) = diff(x)./diff(t); % calculate vx as delta x/delta t
vy(1:end-1) = diff(y)./diff(t); % calculate vy as delta y/delta t

vx(end) = vx(end-1);
vy(end) = vy(end-1);

end
10 changes: 10 additions & 0 deletions lecture_05/nitrogen_pressure.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function P=nitrogen_pressure(v,T)
% function to calculate Pressure of Nitrogen using ideal gas law given the specific
% volume, v (m^3/kg), and temperature, T (K)
% Pv = RT
% R=0.2968; % kJ/kg-K
% P [in kPa] = nitrogen_pressure(v [in m^3/kg], T[in K])
R=0.2968; % kJ/kg-K
P=R*T./v;
end

Binary file modified lecture_05/octave-workspace
Binary file not shown.
Binary file added lecture_05/q1.png
Loading
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/q2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions lecture_05/setdefaults.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(0, 'defaultAxesFontSize', 16)
set(0,'defaultTextFontSize',14)
set(0,'defaultLineLineWidth',3)
Loading