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

5 visualizzazioni (ultimi 30 giorni)
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! :)

Risposta accettata

KSSV
KSSV il 24 Nov 2021
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)

Più risposte (0)

Categorie

Scopri di più su Animation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by