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

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
Jan
2019년 1월 23일
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
2019년 1월 23일
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
2019년 1월 23일
채택된 답변
추가 답변 (3개)
Seif Eldein Zahran
2019년 1월 23일
0 개 추천
댓글 수: 1
Image Analyst
2019년 1월 24일
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
2019년 1월 23일
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

카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!