How do I nicely represent the final answer?

Problem: On a flat field a canon ball with mass m is fired under an angle θ with an initial velocity of v0. Example 1.8 showed that the horizontal distance is R = v0^2 / g * sin 2θ. So the optimal angle is equal to π/4: then R = v0^2 /g. Solve the same problem in case m = 1 kg, v0 = 10 m/s, g = 9.81 m/s^2, but now including a friction force equal to: Ð→ F w = −γÐ→vÐ→v = −γvÐ→v , γ = 0.1. Find λ in θ = λπ/4 for which the distance is maximal. For every correct digit in the value of λ you score 0.1 point with a maximum of 0.5 in total. The value of λ starts with 0, for you to find p1, p2, p3, p4, p5 in λ = 0.p1p2p3p4p5, with every 0 ≤ pk ≤ 9.
My code:
v0 = 10
gamma = 0.1
for k=1:101
theta = 0.8444 * pi/4 + (k-51)*0.0001;
dt = 0.0000001;
x = 0;
y = 0;
vx = v0 * cos(theta);
vy = v0 * sin(theta);
x = x + dt * vx;
y = y + dt * vy;
while (y>0)
v = sqrt(vx*vx+vy*vy);
vx = vx - dt * gamma * vx;
vy = vy - dt * 9.81 - dt * gamma * vx;
x = x + dt * vx;
y = y + dt * vy;
end;
t(k) = theta
a(k) = x
end;
plot(a)
[vv, jv] = max(a)
t(jv) / (pi/4)

8 Kommentare

Bob Thompson
Bob Thompson am 28 Okt. 2019
What do you mean by 'nicely represent?' This seems like an asthetic question, which is very arbitrary, in my opinion.
The Merchant
The Merchant am 28 Okt. 2019
So that i represent the final value of λ in a good way
Bob Thompson
Bob Thompson am 28 Okt. 2019
Bearbeitet: Bob Thompson am 28 Okt. 2019
That didn't really answer my question. What is wrong with having lambda just output as a variable? Do you want it output in a file? Do you want it displayed in a text message? Do you want to plot the values? Do you want to make a GUI for it? Do you want to post it to a website?
The Merchant
The Merchant am 28 Okt. 2019
I want to post it as a string, like: The value of λ is: 0.p1p2p3p4p5etc
Bob Thompson
Bob Thompson am 28 Okt. 2019
I would recommend fprintf or sprintf for that.
The Merchant
The Merchant am 28 Okt. 2019
how would i extract teh value tho
Bob Thompson
Bob Thompson am 28 Okt. 2019
Make a variable for it. I can't really do that for you because I don't really know what lambda represents for your code. I assume it's an angle, because you have theta and are using cos and sin, but that's about all I know.
Original question by OP in case it is deleted (this user has deleted many questions after being answered).
------------------------------------------------------------------
Problem: On a flat field a canon ball with mass m is fired under an angle θ with an initial velocity of v0. Example 1.8 showed that the horizontal distance is R = v0^2 / g * sin 2θ. So the optimal angle is equal to π/4: then R = v0^2 /g. Solve the same problem in case m = 1 kg, v0 = 10 m/s, g = 9.81 m/s^2, but now including a friction force equal to: Ð→ F w = −γÐ→vÐ→v = −γvÐ→v , γ = 0.1. Find λ in θ = λπ/4 for which the distance is maximal. For every correct digit in the value of λ you score 0.1 point with a maximum of 0.5 in total. The value of λ starts with 0, for you to find p1, p2, p3, p4, p5 in λ = 0.p1p2p3p4p5, with every 0 ≤ pk ≤ 9.
My code:
v0 = 10
gamma = 0.1
for k=1:101
theta = 0.8444 * pi/4 + (k-51)*0.0001;
dt = 0.0000001;
x = 0;
y = 0;
vx = v0 * cos(theta);
vy = v0 * sin(theta);
x = x + dt * vx;
y = y + dt * vy;
while (y>0)
v = sqrt(vx*vx+vy*vy);
vx = vx - dt * gamma * vx;
vy = vy - dt * 9.81 - dt * gamma * vx;
x = x + dt * vx;
y = y + dt * vy;
end;
t(k) = theta
a(k) = x
end;
plot(a)
[vv, jv] = max(a)
t(jv) / (pi/4)

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Gefragt:

am 28 Okt. 2019

Kommentiert:

am 15 Dez. 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by