Need help with for loop and ploting.
조회 수: 1 (최근 30일)
이전 댓글 표시
This is the question
Problem 1: Use “for loop” write MATLAB program to calculate function f1, and plot function f1 = cos(x1), for x1 in the domain [0, pi]. (Choose increment of x1 to be 0.2)
Please include (a) Flow Chart (b) Matlab Script (c) A copy of the final Output (plot)
this is what I was able to come up with, can't seem to figure this out with a for loop.
for x1=0:.1:pi f1=cos(x1); f1=[x1,f1];
end hold on plot(x1,f1,'g')
What am I missing?
댓글 수: 0
채택된 답변
Star Strider
2014년 11월 2일
I would code it differently, but there are two changes you would need to make in your code:
f1 = []; % Initialise ‘f1’ As Empty
for x1=0:.1:pi
f=cos(x1); % Use a Different Variable Here
f1=[f1 f]; % Concatanate the New Value With Previous
end
Reproduce your ‘x1’ vector in your for loop statement to plot your function.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!