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

조회 수: 5 (최근 30일)
Joni-Lulu Thomas
Joni-Lulu Thomas 2021년 11월 24일
답변: KSSV 2021년 11월 24일
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