01_ME3255_repo
First repository (first homework assignment) for ME3255
HW 2 table
solver | initial guess(es) | ea | number of iterations |
---|---|---|---|
falsepos | 0 1 | 0.01 | 21 |
mod_secant | 1 | 0.009 | 5 |
bisect | 0 1 | 0.011 | 19 |
#Answer to Homework Question I hope to learn more matlab tricks and about the backgrounds and goals of engineering students.
#Problem 3 - first script for loops
A_66=zeros(6,6);
for i=1:6
for j=1:6
A_66(i,j)=i*j;
end
end
fprintf('mean of A_66 =%1.2f\nstdev of A_66=%1.2f\n',mean(A_66(:)),std(A_66(:)))
output:
-
mean of A_66 = 12.25
-
stdev of A_66 = 9.07
#Problem 4 - US Energy Consumption
mean=mean(A_66(:));
mode=mode(A_66(:));
std=std(A_66(:));
set(0, 'defaultAxesFontSize', 16);
set(0, 'defaultAxesFontSize', 16);
set(0, 'defaultLineLineWidth', 3);
figure(1)
data=dlmread('US_energy_by_sector.csv',',',2,0);
data(1,:);
plot(data(:,1), data(:,3), data(:,1), data(:,9)) %residential in column 3 %transportation in column 9
xlabel('Year');
ylabel('Trillions of BTU');
legend('Residential', 'Transportation', 'Location', 'NorthWest');
title('US Energy Consumption');
size(data);
print('figure_01.png','-dpng');
figure(2)
plot(data(:,1), cumtrapz(data(:,3)/(10^6)), data(:,1), cumtrapz(data(:,9)/(10^6))) %residential in column 3 %transportation in column 9
xlabel('Year');
ylabel('Quintillions of BTU');
legend('Residential', 'Transportation', 'Location', 'NorthWest');
title('Cumulative US Energy Consumption');
size(data);
print('figure_02.png','-dpng');
#Problem 5 - Freefall
function [v_terminal,t]=freefall_comparison(h, timespan)
% help file for freefall_comparison.m
% N is number of timesteps between 0 and 12 sec
% v_numerical is numerical approximation
% Enter "freefall_comparison(0.1,30)" to initiate program
t=(0:h:timespan);
c=0.25; m=60; g=9.81; v_terminal=sqrt(m*g/c)
v_numerical=zeros(length(t),1);
delta_time =diff(t);
for i=1:length(t)-1
v_numerical(i+1)=v_numerical(i)+(g-c/m*v_numerical(i)^2)*delta_time(i);
end
plot(t,v_numerical,'-');
hold on
h=1;
t=(0:h:timespan);
c=0.25; m=60; g=9.81; v_terminal=sqrt(m*g/c)
v_numerical=zeros(length(t),1);
delta_time =diff(t);
for i=1:length(t)-1
v_numerical(i+1)=v_numerical(i)+(g-c/m*v_numerical(i)^2)*delta_time(i);
end
plot(t,v_numerical,'-');
h=5;
t=(0:h:timespan);
c=0.25; m=60; g=9.81; v_terminal=sqrt(m*g/c)
v_numerical=zeros(length(t),1);
delta_time =diff(t);
for i=1:length(t)-1
v_numerical(i+1)=v_numerical(i)+(g-c/m*v_numerical(i)^2)*delta_time(i);
end
plot(t,v_numerical,'-');
legend('h=0.1','h=1','h=5','Location','southeast');
print('figure_03.png','-dpng')
end
xlabel('Time (Seconds)');
ylabel('Calculated Velocities v(t)');
title('Calculated Velocities over Time');
legend('h=0.1','h=1','h=5','Location','southeast');
print('figure_03.png','-dpng')
#Problem 6 - Velocity and Acceleration
function [vx,vy,vz] = my_velocity(x,y,z,t)
% Help documentation of "my_velocity"
% This function computes the velocity in the x-, y-, and z-directions given
% three vectors of position in x-, y-, and z-directions as a function of time
% x = x-position
% y = y-position
% z = z-position
% t = time
% output
% vx = velocity in x-direction
% vy = velocity in y-direction
% vz = velocity in z-direction
vx=zeros(length(t),1)';
vy=zeros(length(t),1)';
vz=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
vz(1:end-1) = diff(z)./diff(t); % calculate vz as delta z/delta t
vx(end) = vx(end-1);
vy(end) = vy(end-1);
vz(end) = vz(end-1);
% In the command window, the user must enter vectors for x, y,
% z, and t, in either lists of numbers in square brackets or
% parentheses and colons. An example is shown below:
%[vx,vy,vz]=my_velocity((1:3),[2 4 5],(2:4),(6:8))
end
function [ax,ay,az]=my_acceleration(x,y,z,t)
% Help documentation of "my_acceleration"
% This function computes the acceleration in the x-, y-, and z-directions given
% three vectors of position in x-, y-, and z-directions as a function of time
% x = x-position
% y = y-position
% z = z-position
% t = time
% output
% ax = acceleration in x-direction
% ay = acceleration in y-direction
% az = acceleration in z-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,vz]=my_velocity(x,y,z,t);
ax = diff_match_dims(vx,t);
ay = diff_match_dims(vy,t);
az = diff_match_dims(vz,t);
% In the command window, the user must enter vectors for x, y,
% z, and t, in either lists of numbers in square brackets or
% parentheses and colons. An example is shown below:
%[ax,ay,az]=my_acceleration((1:3),[2 4 5],(2:4),(6:8))
end