Help about a MATLAB Question
이전 댓글 표시
Hi everyone. I just started to learn MATLAB. So I'm trying to develop myself with solving problems. And here is a question in the PDF that I could not solve because there are a lot of variables in the equation and I can't connect them each other and write the while loops. I would be grateful to you if you solved this question. Thanks a lot!
답변 (3개)
Image Analyst
2016년 12월 28일
OK Alper, assuming this is just your self-learning and I'm not doing your hojmework for you, try this code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
T = 0.001;
C = 0.008;
t = 0 : T : 16;
A = 30 * ones(1, length(t));
v = zeros(1, length(t));
n = 1;
while n <= length(t)
v(n+1) = v(n) + T * (A(n) - C * v(n) ^ 2);
% Break when we hit 60
if v(n+1) >= 60
% Save the index and time.
t60 = t(n+1)
n60 = n;
break; % Quit the loop.
end
n = n + 1;
end
plot(t(1:n60), v(1:n60), 'b-', 'LineWidth', 2);
grid on;
title('Velocity', 'FontSize', fontSize);
xlabel('Time', 'FontSize', fontSize);
ylabel('Velocity', 'FontSize', fontSize);
hold on;
% Plot red circle.
plot(t(n60), v(n60), 'ro', 'MarkerSize', 15, 'LineWidth', 2);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Now decelerate.
% Make acceleration -1 from that point on
A(n60+1:end) = -2;
n = n60;
nStop = length(t);
tStop = t(nStop);
while n <= length(t)
v(n+1) = v(n) + T * (A(n) - C * v(n) ^ 2);
% Break when we hit 0
if v(n+1) <= 0
% Save the index and time.
tStop = t(n+1)
nStop = n;
break; % Quit the loop.
end
n = n + 1;
end
plot(t(1:nStop), v(1:nStop), 'b-', 'LineWidth', 2);
grid on;
ylim([0, 80]);
title('Velocity', 'FontSize', fontSize);
xlabel('Time', 'FontSize', fontSize);
ylabel('Velocity', 'FontSize', fontSize);
% Plot red square.
plot(t(nStop), v(nStop), 'rs', 'MarkerSize', 15, 'LineWidth', 2);
% Put up legend.
textFontSize = 23;
text(10, 75, '--- v(t)', 'Color', 'r', 'FontSize', textFontSize);
caption = sprintf('o = t_6_0 = %.2f sec', t60);
text(10, 70, caption, 'Color', 'r', 'FontSize', textFontSize);
% Plot square
text(10, 65.7, char(hex2dec('25a1')), 'Color', 'r', 'FontSize', textFontSize);
caption = sprintf(' = t_S_t_o_p = %.2f sec', t(nStop));
text(10.2, 65, caption, 'Color', 'r', 'FontSize', textFontSize);

댓글 수: 2
Alper Camci
2016년 12월 28일
편집: Alper Camci
2016년 12월 29일
Image Analyst
2016년 12월 28일
I can't help you with any Simulink followup questions, but if my answer to this one solved your question, then can you "Accept this answer"? Thanks in advance.
Majed Mabadi
2020년 10월 11일
0 개 추천
Create a variable a that is a row vector with the following elements:9,1,3^2,7/4,0,2.25*8.5,0.8,and sin(pi/8)
댓글 수: 2
Walter Roberson
2020년 10월 11일
That does not appear to be an Answer to this question?
Image Analyst
2020년 10월 11일
You should have posted this in your own, separate question, not as an answer to Alper's question. If you had, someone probably would have given you the answer
a = [9, 1, 3^2, 7/4, 0, 2.25*8.5, 0.8, sin(pi/8)]
or possibly this:
카테고리
도움말 센터 및 File Exchange에서 General Applications에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!