plotting in a loop with a function

1 次查看(过去 30 天)
I have the following code:
for x = 1:0.01:1.5
z = g(x,rpf);
end
but when I run this (assume my function is correct), I only get that z is equivalent to the last data point. How can I store x and z in a matrix and then plot them?

采纳的回答

Bob Thompson
Bob Thompson 2019-3-5
You need to index z with each loop.
count = 0;
for x = 1:0.01:1.5;
count = count + 1;
z(count) = g(x,rpf);
end
plot(x,z)
  7 个评论
Benjamin
Benjamin 2019-3-5
编辑:Benjamin 2019-3-5
Can I ask a follow-up? How could I do an outer loop for different values of rpf and then plot each one on the same plot? And the values for rpf would be like specific values: (i.e. 5.4, 6.7, 5.2 etc) Any ideas how I would do this? When I try it, it just keeps adding move rows to z, rather than just overwriting it on the next loop
Benjamin
Benjamin 2019-3-5
I created a new question, maybe you could answer this question there.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by