How do I refer to the next y point in a function?
1 view (last 30 days)
Show older comments
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! :)
0 Comments
Accepted Answer
KSSV
on 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
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 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!