Numercial Iterations using Loop
Show older comments
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

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
Jan
on 23 Jan 2019
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?
Image Analyst
on 23 Jan 2019
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.
Seif Eldein Zahran
on 23 Jan 2019
Accepted Answer
More Answers (3)
Seif Eldein Zahran
on 23 Jan 2019
0 votes
1 Comment
Image Analyst
on 24 Jan 2019
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
Image Analyst
on 23 Jan 2019
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

Seif Eldein Zahran
on 23 Jan 2019
0 votes
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!