homework_1
Answer to homework question
I hope to learn more about Matlab and how to make it useful for me as an engineer
I worked on trying to get this to be the matlab code and followed all the directions on how to make it appear as matlab code on github but it would never change to appear as the matlab code.
Problem 3
'''matlab 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(:))) '''
Outputs: mean of A_66 =12.25 stdev of A_66 =9.07
Problem 4
'''matlab data=dlmread('US_energy_by_sector.csv',',',2,0); h=figure(); plot(data(:,1),data(:,3),data(:,1),data(:,9)) legend('Residential','Transportation','Location','Northwest') xlabel('years') ylabel('trillions of Btus') saveas(h,'problem4.png') '''
Problem 5
'''matlab function [v_analytical,v_terminal,t]=freefall(h,timespan) % help file for freefall.m % N is number of timesteps between 0 and 12 sec % v_an... % t=linspace(0,12,N)'; t=(0:h:timespan)'; c=0.25; m=60; g=9.81; v_terminal=sqrt(m*g/c);
v_analytical = v_terminal*tanh(g*t/v_terminal);
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
% Print values near 0,2,4,6,8,10,12 seconds
f=figure();
indices = round(linspace(1,length(t),7));
fprintf('time (s)|vel analytical (m/s)|vel numerical (m/s)\n')
fprintf('-----------------------------------------------\n')
M=[t(indices),v_analytical(indices),v_numerical(indices)];
fprintf('%7.1f | %18.2f | %15.2f\n',M(:,1:3)');
plot(t,v_analytical,'-',t,v_numerical,'o-')
saveas(f,'Problem5.png')
Freefall comparison '''matlab c=0.25; m=60; g=9.81; v_terminal=sqrt(mg/c); h1 = 0.1; timespan = 30 ; t1=(0:h1:timespan)'; v_analytical1 = v_terminaltanh(gt1/v_terminal); h2 = 1 ; t2=(0:h2:timespan)'; v_analytical2 = v_terminaltanh(gt2/v_terminal); h3 = 5 ; t3=(0:h3:timespan)'; v_analytical3 = v_terminaltanh(g*t3/v_terminal); f = figure() plot(t1, v_analytical1,'-', t2, v_analytical2,'o-', t3,v_analytical3,'--') saveas(f, 'problem5.png') '''
Problem 6
'''matlab function [vx,vy,vz] = my_velocity(x,y,z,t) % Help documentation of "my_velocity" % 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);
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);
vx(end) = vx(end-1);
vy(end) = vy(end-1);
vz(end) = vz(end-1);
end
function [ax,ay,az]=my_acceleration(x,y,z,t) % Help documentation of "my_acceleration" % 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,vz]=my_velocity(x,y,,zt);
ax = diff_match_dims(vx,t);
ay = diff_match_dims(vy,t);
az = diff_match_dims(vz,t);
end '''