Title | Matlab - Exercise 22 - Throwball Solution |
---|---|
Course | MATLAB Boot Camp |
Institution | Harvard University |
Pages | 3 |
File Size | 34.2 KB |
File Type | |
Total Downloads | 98 |
Total Views | 125 |
Download Matlab - Exercise 22 - Throwball Solution PDF
% Exercise 2.2 Solutions %% 1. x=[]; for i=1:2:10 x(i)=i; end x % The loop runs 5 times, and X=[1 0 3 0 5 0 7 0 9] %% 2. % In MATLAB, loops are sometimes necessary, but if possible, code runs % faster when loops are replaced by vector operations. Can you re-write % the loops in this section in vector form? % loops.m: a= [0 52 25 231]; a % loops2.m: (1:10).^2 % loops3.m: a=(1:10).^2; a % loops4.m: a=(1:10).^2 % loops1b.m: a = [0 1; 52 2; 25 1; 231 3]; a(:,1) a(:,2) % loops5.m: a=1:100; b = a(mod(a,10)==3) %3. throwBall with different initial conditions % this is a script that throws a ball of a set mass at various initial % heights and initial velocities, and plot these different situations into subplots close all % define initial conditions h=[1.5, 0.75, 1.5, 1.5]; %meters
v=[4,4,2,4]; %m/s theta=[45, 45, 45, 30];% degrees % define the constants g=9.8; %gravitational acceleration in m/s^2 % make a time vector, and make empty x and y vectors t=linspace(0,1,1000); x=zeros(4,length(t)); y=zeros(4,length(t)); % Start the loop for i=1:4 % calculate the x and y positions as a function of time x(i,:)=v(i)*cos(theta(i)/180*pi)*t; y(i,:)=h(i)+ v(i)*sin(theta(i)/180*pi)*t-1/2*g*t.^2; % find when it hits the ground ind=find(y(i,:)...