a)
Code Block |
---|
function [vx , vy] = initialVelocityinitVelocity(initialAngle, speedinitialSpeed) vx = coscosd(initialAngle)*speedinitialSpeed; vy = sinsind(initialAngle)*speedinitialSpeed; end |
b)
Code Block |
---|
function [x, y] = position(x,y, vx, vy, dt) x = x + vx*dt; y = y + vy*dt; end |
c)
Code Block |
---|
function [ ax , ay ] = acceleration (vx ,vy)
k = 0.01;
g = 9.81;
ax = -k*vx*abs(vx);
ay = -k*vy*abs(vy)-g;
end |
d)
Code Block |
---|
function [vx , vy] = velocity(ax, ay, vx, vy, dt)
vx = vx + ax * dt;
vy = vy + ay * dt;
end |
Code Block |
---|
|
Code Block |
---|
|
...
e)
Code Block |
---|
function [x, y] = trajectory(initialSpeed, initialAngle, height)
dt =0.01;
xPos = 0; % Nåværende verdi for x
yPos = height ; % Nåværende verdi for y
[vx , vy] = initVelocity(initialAngle ,initialSpeed);
i = 1;
while yPos > 0
% kalkuler akselerasjon
[ax , ay] = acceleration(vx, vy);
% kalkuler fart
[vx , vy] = velocity(ax, ay, vx, vy, dt);
% kalkuler endring i distanse
[xPos, yPos] = position (xPos, yPos, vx, vy, dt);
x(i) = xPos;
y(i) = yPos;
i = i + 1;
end
end |
f)
Code Block |
---|
function plotTrajectory(initialSpeed, initialAngle, height)
[x, y] = trajectory(initialSpeed, initialAngle, height);
plot (x, y);
grid on;
xlabel('x');
ylabel('y');
title('plotTrajectory');
end |
g)
Code Block |
---|
function plotTrajectoryLength (initialSpeed, start, step, stop, height)
initialAngles = start:step:stop;
n = length(initialAngles);
lengths = zeros(n,1);
for i = 1:n
[x, y] = trajectory(initialSpeed, initialAngles(i), height);
j = length (x);
lengths (i) = x(j);
end
plot (initialAngles ,lengths);
xlabel('Angle');
ylabel('y');
grid on;
title('plotTrajectoryLength');
end |
Det kan virke som om pi/4 (45 grader) gir lengst støt. Dette virker intuitivt korrekt.
Jo høyere man kaster fra, jo bedre er det å kaste mest mulig rett frem (i x-retning).