Numercial Iterations using Loop

function [vAn,vNum] = Q3_STNO(T,N,m,d)
T = 5
N = 10
m = 5
d = 0.5
Cd = 0.47
p = 1.2
A = (pi)*((d/2)^2);
g = 9.81;
Terminal_V = -sqrt((2*m*g)/p*Cd*A)
V= -Terminal_V*tanh(g*T/Terminal_V)
acc = (((Cd*p*A)/2*m))*(V^2)-g
for acc = 1:1:T
V = V + acc*(T/N)
plot(T,V)
end
end
Untitled.jpg
I have written my codes several times , its been 1 week now and I still cannot get the code to work. Any help would be extremely appreciated.
Seif

3 Comments

We cannot guess, what the problem is. Please explain, what "cannot get code to work" means. What do you want to achieve and what is not working yet?
You need "hold on" after the call to plot. And you need to plot (acc, V) to plot that point, NOT plot(T, V). There may be other problems but that's what I saw off the top of my head.
Jan I am writing a code that calculates the velocity of a ball after a given time released by using the equations in the picture above. What I am trying to do is to approximate velocities sequentially using a numerical method:
vn+1 = vn + anδt
where vn and an approximate the velocity and acceleration at time tn (= nδt) respectively, and δt is the time step between values
with the function format being [vAn,vNum] = Q3 STNO(T,N,m,d)
where T is the given time in seconds, N is the number of time steps used for the numerical method (such that δt = T/N), m is the mass of the ball in kilograms, d is the diameter of the ball in metres and vAn and vNum are the analytical and numerical value for the velocity at time T, respectively
I just need help regarding the numerical method.

Sign in to comment.

 Accepted Answer

This is strange:
function [vAn,vNum] = Q3_STNO(T,N,m,d)
You do not create the outputs vAn and vNum. The inputs T,N,m,d are overwritten immediately, then it is better to omit them. Or define them by using the inputs, as mentioned in the question.
function Main
T = 5; % Trailing semicolons suppress the output to the command window
N = 10;
m = 5;
d = 0.5;
[vAn,vNum] = Q3_STNO(T,N,m,d)
end
function [vAn, vNum] = Q3_STNO(T,N,m,d)
Cd = 0.47;
p = 1.2;
A = pi *(d/2)^2; % Do not use too many parentheses
g = 9.81;
vEnd = -sqrt(2 * m * g / (p * Cd * A)); % Here you need parentheses
% around the denominator
vAn = -Terminal_V * tanh(g * T / Terminal_V);
% The acceleration is not fixed, so you need it inside the loop:
% acc = (((Cd*p*A)/2*m))*(V^2)-g
% In addition: Missing parentheses around (2*m)!
% It is not the acceleration which goes from 1 to T
% for acc = 1:1:T
% You need T/N steps for the time. Usually the time starts at 0.
V = 0; % what is the initial velocity?
plot(0, V, '.');
hold on
for t = 0:T/N:T
acc = Cd * p * A / (2 * m) * V^2 - g;
V = V + acc * (T / N);
plot(t, V);
end
vNum = V;
end
You need some more details according to the question, and I leave this up to you.

More Answers (3)

Imaga Analyst, what do you mean "hold on" ? and I actually need to plot velocity with time according to the Numerical method.

1 Comment

Since your T and V were just scalars, not arrays, plot(T, V) will plot just a single point. However each call to plot blows away everything prior and plots only the current points. To have it not blow away prior things that were plotted, you need to put hold on. If you call hold on after plot() then your prior points will remain and you will see all of them
If you're in a loop, the message to update/refresh the plot will probably not be acted upon until the loop is finished. If you want to see every point as it's plotted you might want to call drawnow() followed by a pause() to slot it down enough that it looks like an animation:
for index = 1 : T
V = V + ....whatever
T = ....... whatever
plot(T,V); % Plot one single point.
hold on; % Don't blow away prior points.
drawnow; % Force screen to refresh immediately instead of after loop.
pause(0.5); % Wait long enough so that points are plotted slowly and it looks like an animation.
end

Sign in to comment.

Here is a bit closer. Still not right yet, but closer. If you still can't complete it, let us know.
% Call the function from the main routine
T = 5
N = 10
m = 5
d = 0.5
[vAn,vNum] = Q3_STNO(T,N,m,d)
% Define the function that the main routine will call.
function [vAn,vNum] = Q3_STNO(T,N,m,d)
vAn= 0; % Initialize
vNum = 0; % Initialize
Cd = 0.47
p = 1.2
A = (pi)*((d/2)^2);
g = 9.81;
Terminal_V = -sqrt((2*m*g)/p*Cd*A)
V(1) = -Terminal_V*tanh(g*T/Terminal_V)
acc = (((Cd*p*A)/2*m))*(V^2)-g
for index = 2 : T
V(index) = V(index-1) + acc * (T/N)
end
index = 1 : T;
plot(index, V, 'b+-', 'LineWidth', 2, 'MarkerSize', 15)
xlabel('index', 'FontSize', 14);
ylabel('V', 'FontSize', 14);
grid on;
end
0000 Screenshot.png
Jan and Image Analyst , Thankyou guys so much that has helped me a great deal.

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!