I can't plot.

for t=0:10
disp(t);
x=-.196*t;
disp(x);
c=-40:20:20;
disp(c)
v=50+(c*exp(x));
disp(v);
end
plot(t,v)

1 Commento

Rik
Rik il 14 Ago 2017
What are you trying to do? There are many thing that could be wrong with this code.
Add some detail to this question, show what you tried in a readable way: use the {}Code button.
Have a read here and here. It will greatly improve your chances of getting an answer.

Accedi per commentare.

Risposte (1)

Val Kozina
Val Kozina il 14 Ago 2017

2 voti

In your code, 't' and 'v' are just scalars, not vectors. At the end of the loop, 't' will simply have the value 10, and 'v' will be the last calculated value. If you are trying to plot the values of 'v' for each 't' from 0 to 10, you should consider declaring these variables as vectors before the loop. You can also declare 'c' outside of the loop since its value never changes, allowing you to preallocate the right amount of space for 'v'. Then, simply iterate over the values in vector 't', and record the result into the next spot in 'v' (note that since matrix indices must start at 1 you will have to index using the current value of t + 1). Altogether your code would look like this:
t = 0:10;
c=-40:20:20;
v = zeros(numel(t), numel(c));
for cur_t = t
disp(cur_t);
x=-.196*cur_t;
disp(x);
v(cur_t+1, :)=50+(c*exp(x));
end
plot(t,v)
This should result in 4 separate lines on a single plot, one for each value of c.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Richiesto:

il 14 Ago 2017

Commentato:

il 14 Ago 2017

Community Treasure Hunt

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

Start Hunting!

Translated by