# Projectile Motion

Solution

myfunc.m

function [x y] = myfunc(y0, v0, theta)

t = 0:0.001:1;

g = 9.8;

out1 = v0 * cos(theta * pi / 180) * t;

out2 = y0 + v0 * sin(theta * pi / 180) * t – 1 / 2 * g * t.* t;

hfig = figure(‘Name’,’Rock Projectile’);

hax = axes(‘Parent’,hfig);

plot(hax,t,out1,’k-‘,’LineWidth’,2.0,’Color’,’red’);

hold on

plot(hax,t,out2,’k-‘,’LineWidth’,1.0,’Color’,’blue’);

hold on

plot(hax,[0 1],[0 0],’Color’,’black’);

xlabel(‘Time’,’FontWeight’,’bold’);

ylabel(‘X(red) and Y(blue)’,’FontWeight’,’bold’);

x = out1;

y = out2;

end

test_projectile.m

% test the projectile

y0 = 1.5;

v0 = 4.5;

theta = 30;

[x y] = myfunc(y0,v0,theta);

external_fun_1.m

function [out]=external_fun_1(x)

%evaluate the expression using the input argument

y = 1.5 * cos(x) + 6 * exp(-0.1 * x) + exp(0.07 * x).*sin(3*x);

%Calculate the mean and display its value.

ym = mean(y); disp(ym)

%Create a figure, axes parented to that axes

close all;

hfig = figure(‘Name’,’Function and Mean’);

hax = axes(‘Parent’,hfig);

plot(hax,x,y,’k-‘,’LineWidth’,2.0)

%Hold the current plot and add a red line along the mean value.

hold on

plot(hax,[min(x) max(x)],[ymym],’Color’,’red’);

%hold off

hold on

%Add a tick lavel that shows the mean value

%and add a title and axis labels

ylab = get(hax,’YTick’);

set(hax,’YTick’,sort([ylabym]))

title(‘y = 1.5cos(x) + 6e^{-0.1x} + e^{0.07x}sin{3x}’,’FontSize’,12,’FontWeight’,’bold’)

xlabel(‘X Axis’,’FontWeight’,’bold’);

ylabel(‘Y Axis’,’FontWeight’,’bold’);

hold off

out = y;

end

TensileData.m

classdefTensileData

properties

Material = ‘carbon steel’;

SampleNumber

Stress

Strain

end

properties (Dependent = true, SetAccess = private)

Modulus

end

methods

function td = TensileData(material,samplenum,stress,strain)

ifnargin> 0

td.Material = material;

td.SampleNumber = samplenum;

td.Stress = stress;

td.Strain = strain;

end

end % TensileData

end

methods

functionobj = set.Material(obj,material)

if ~(strcmpi(material,’aluminum’) ||…

strcmpi(material,’stainless steel’) ||…

strcmpi(material,’carbon steel’))

error(‘Material must be aluminum, stainless steel, or carbon steel’)

end

obj.Material = material;

end % Material set function

function modulus = get.Modulus(obj)

ind = find(obj.Strain> 0); % Find nonzero strain

modulus = mean(obj.Stress(ind)./obj.Strain(ind));

end % Modulus get function

functionobj = set.Modulus(obj,~)

fprintf(‘%s%d\n’,’Modulus is: ‘,obj.Modulus)

error(‘You cannot set Modulus explicitly’);

end

functiondisp(td)

fprintf(1,’Material: %s\nSample Number: %g\nModulus: %1.5g\n’,…

td.Material,td.SampleNumber,td.Modulus);

end % disp

function plot(td,varargin)

plot(td.Strain,td.Stress,varargin{:})

title([‘Stress/Strain plot for Sample ‘,num2str(td.SampleNumber)])

xlabel(‘Strain %’)

ylabel(‘Stress (psi)’)

end % plot

end

methods (Access = ‘private’) % Access by class members only

function m = CalcModulus(td)

% Over-simplified calculation of Elastic Modulus

ind = find(td.Strain> 0); % Find nonzero strain

m = mean(td.Stress(ind)./td.Strain(ind));

end % CalcModulus

end

end % classdef

test_external_func_1.m

% test of the external function

x = -10:0.005:40;   %definition of the array

y = external_fun_1(x);

test_external_func_2.m

%Test of the external funciton

%Material

TensileData.Material = ‘carbon steel’;

%Samplenumber

TensileData.SampleNumber = 001;

% Array values for stress

TensileData.Stress = [33.0948 40.6791 42.7475 44.1264 45.5054 48.2633 57.9160 60.6739 65.5002];

%Array values for strain

TensileData.Strain = [0.16 0.17 0.18 0.24 0.26 0.28 0.30 0.32 0.33];

%Modulus

TensileData.Modulus = mean(TensileData.Stress./TensileData.Strain);

%Excutation

TensileData.Modulus

%plot

p = polyfit(TensileData.Strain, TensileData.Stress, 1);

y = polyval(p, TensileData.Strain);

plot(TensileData.Strain, y)

xlabel(‘Strain (%)’)

ylabel(‘Stress (psi)’)

%Percent difference

diff = (TensileData.Modulus – p)/TensileData.Modulus *100

%Plot data and fitted curve

plot(TensileData.Strain, TensileData.Stress, ‘-r’, TensileData.Strain, y, ‘b’)

xlabel(‘Strain (%)’)

ylabel(‘Stress (psi)’)

title(‘Stress and Strain’)

legend(‘fitted curve’, ‘plot data’)

test_external_func_3.m

%Rotation of strees tensor (2D)

function test_external_func_3

%Original frame

round = [3e3 2e3;2e3 4e3];

%rotation frame by alpha = 30 counter-clockwise

round_dot = round.*cos(pi/6)