Evaluate f(x) for multiple evenly spaced values

조회 수: 34 (최근 30일)
Josh
Josh 2013년 2월 16일
How do I evaluate a function f(x) for multiple, evenly spaced values of x between two variables. ie x = exactly 10 evenly spaced numbers between 5 and 20
I'm thinking define an anonymous function so I can evaluate it over a range of values but how to set those values is where I'm lost

채택된 답변

Shashank Prasanna
Shashank Prasanna 2013년 2월 16일
x = linspace(5,20,10); % Generate 10 evenly spaced points between 5 and 20
y = f(x); % Call your function, assuming you have defined it already
%If f can't take in a vector you will have to call it in a loop.
  댓글 수: 3
Youssef  Khmou
Youssef Khmou 2013년 2월 16일
Josh, to use mpower in your example, you must use elementwise :
f=@(x) x.^3-2*x.^2-3*x+1
x=linspace(5,20,10);
y=feval(f,x)
Image Analyst
Image Analyst 2013년 2월 16일
편집: Image Analyst 2013년 2월 16일
Josh, you have to do y=f(x), not f(x)=y. The "target" variable is on the left, and the expression on the right is evaluated and stuffed into the variable on the left. This is how every programming language that I've used does it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Youssef  Khmou
Youssef Khmou 2013년 2월 16일
hi,
You can set the values using 'linspace' :
x=linspace(5,20,10);
f=@(x) sin(x)
y=feval(f,x);
  댓글 수: 1
Youssef  Khmou
Youssef Khmou 2013년 2월 16일
linspace(a,b,n) generates a vector of n points linearly spaced between and including a and b .

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by