How do I get this to actually plot?

조회 수: 1 (최근 30일)
Samantha Fesler
Samantha Fesler 2016년 2월 7일
댓글: Samantha Fesler 2016년 2월 7일
I am taking a class and I was asked to create a for loop and a while loop. I figured out how to do those and know that they work. The teacher gave us a code template to use and for most of the assignment it's been great. However, for this section the code he provided is for the plotting of the loops. It's the part after the comment "%...Create the plot". I'm still fairly new to Matlab, so I don't know how to modify it to actually show something when I plot it.
Here is the code:
X = 1;
while X<20
F(X) = X^(3)-(5*X)^(2)-2^(X)-10000*X;
X = X+1;
end
f = 0;
for x = 1:19
f(x) = 20000*log(x)-3*x;
end
%...Create the plot
fig = figure(1); clf; grid on; hold on;
xlabel('x1'); ylabel('y1'); title('While Loop');
p = plot(Value(:,1),Store(:,1));
set(p, 'Color', 'red', 'LineWidth', 2);
p = plot(V(:,1), S(:,1));
set(p, 'Color', 'blue', 'LineWidth', 4);
Thanks for the help!

채택된 답변

Image Analyst
Image Analyst 2016년 2월 7일
You need to replace the variable names Value, Store, V, and S with your variable names. What variable names did you use? For example
X = 1:19
plot(X, F, 'r*-', 'LineWidth', 2);
hold on;
x = 1:19
plot(x, f, 'bo-', 'LineWidth', 4);
You don't really need the set()'s - I did it all within the call to plot().
  댓글 수: 1
Samantha Fesler
Samantha Fesler 2016년 2월 7일
I had tried replacing the variable names, but it wasn't working. Reformatting the whole thing like your example though worked perfect! Thanks for the assistance!

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

추가 답변 (2개)

John BG
John BG 2016년 2월 7일
편집: John BG 2016년 2월 7일
the functions of interest are F and f, correct?
your plot effort doesn't render a graph because your attempting to plot 19 sample long vector against a single sample reference vector.
function values and reference values have to be contained in vectors of same length. Generating the reference vectors:
Store=F
nStore=[1:1:length(F)]
S=f
nS=[1:1:length(S)]
Again I am assuming that 'Store' in the plot function is 'F' and 'S' is 'f'. If it's the other way around, just swap them.
The basic plot lines are
figure(1); plot(nStore,Store);grid on
figure(2);plot(nS,S);grid on
Again, Store(:,1) is just one sample, you want to plot all values of Store or a range within Store, but not a single value, do you? so remove (:,1)
instead of Store(:,1) you probably mean Store(1,:)
since
[Store_rows,Store_columns]=size(Store)
Store_rows = 1.00
Store_columns = 19.00
you crossed rows and columns, don't worry we all have gone through this.
there is no need for 'hold on' if you choose to assign an individual graph to each curve with figure(1) and figure(2).
I do not get in labels and other comments you want to add, but:
1. no need for set(..) you can switch plot options within same plot( .. ) have a look to MATLAB help searching: plot
2. Since you just started, perhaps you want to leave the handle(s) that you attempt to collect with variable 'p' for later on, once you are familiar with plot, stem, surf, ..
Hope it helps, nothing like clocking practice hours.
If you find this answer useful, please click on the above thumbs-up Vote, thanks in advance.
John
  댓글 수: 1
Samantha Fesler
Samantha Fesler 2016년 2월 7일
Thanks for explaining some of these commands for me. I'm going to see if I can figure them out and get them to work.

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


Walter Roberson
Walter Roberson 2016년 2월 7일

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by