How do I refer to the next y point in a function?

5 次查看(过去 30 天)
say I have y = f(x), how can I refer to the next point in y(a+1) = f(x+1) with only using y in my code?
Thank you! :)

采纳的回答

KSSV
KSSV 2021-11-24
You need to define x as an array, when you sustitue x in the function f, you will get output array y which is of same dimension like x. And after you can index.
EXamples:
f = @(x) sin(x) ;
x = linspace(0,2*pi) ;
y = f(x) ;
plot(x,y)
for i = 1:10
[x(i) y(i)]
end
ans = 1×2
0 0
ans = 1×2
0.0635 0.0634
ans = 1×2
0.1269 0.1266
ans = 1×2
0.1904 0.1893
ans = 1×2
0.2539 0.2511
ans = 1×2
0.3173 0.3120
ans = 1×2
0.3808 0.3717
ans = 1×2
0.4443 0.4298
ans = 1×2
0.5077 0.4862
ans = 1×2
0.5712 0.5406
If you are planning to use a loop (to learn).
f = @(x) sin(x) ;
x = linspace(0,2*pi) ;
y = f(x) ;
n = length(x) ;
y = zeros(1,n) ;
for i = 1:n
y(i)= f(x(i)) ;
end
plot(x,y)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by