How do I get this to actually plot?
1 view (last 30 days)
Show older comments
Samantha Fesler
on 7 Feb 2016
Commented: Samantha Fesler
on 7 Feb 2016
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!
0 Comments
Accepted Answer
Image Analyst
on 7 Feb 2016
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().
More Answers (2)
John BG
on 7 Feb 2016
Edited: John BG
on 7 Feb 2016
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
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!