Hey, I am new to MATLAB. I am running the following code and cant figure out why the plot shows no data. I know it is showing only the last iteration, but I dont know how to fix it. I have tried using/not using a for loop, I basically need a plot of v vs t. But I would like to learn how to plot results of a for loop. PLEASE HELP:
%for
t= [-5.0:0.5:50.0] %s
if t>=0 & t<=10
v=(11*(t.^2))-(5*t)
elseif t>=10 & t<=20
v=1100-(5*t)
elseif t>=20 & t<=30
v=(50*t)+2*((t-20)^2)
elseif t>30
v=1520* exp(-0.2*(t-30))
else
v=0
end
format long g
%end
%Tab=table(t(:), v(:))
plot(t(:),v(:))
hold on
File is also attached.

 채택된 답변

Adam Danz
Adam Danz 2021년 1월 26일
편집: Adam Danz 2021년 1월 26일

1 개 추천

The solution involves matlab indexing which is a very basic skill to know before making much progress.
This should get you started.
t= [-5.0:0.5:50.0];
v = zeros(size(t));
idx = t>=0 & t<=10;
v(idx) = (11*(t(idx).^2))-(5*t(idx));
idx = t>=10 & t<=20;
v(idx) = 1100-(5*t(idx));
% repeat for the other indices

댓글 수: 2

Shantanu Chatterji
Shantanu Chatterji 2021년 1월 26일
Thanks Mr. Danz!
Does this mean that a for loop/if statement is not required or would this work even if i added for/if statements to this?
Thanks, you are awesome!
Adam Danz
Adam Danz 2021년 1월 26일
This solution does not need any loops or conditional statements.
Indexing acts as conditons. The "idx" variable is a logical vector identifying which values to access and change.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 1월 26일

댓글:

2021년 1월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by