I seem to be getting parse and syntax errors while coding, cant seem to solve them. Please help :-(. Ive included my code below

조회 수: 7 (최근 30일)
function area = simpsonsRule = (f, interval, num_pts);
f = input ('f(x) to integrate');
interval = input ('[a,b]');
num_pts = input ('points to be evaluated');
f(x)=f;
n=num_pts;
a=min(interval);
b=max(interval);
h=(b-a)/n;
outer_func = (f*a+f*b);
for i = 2:2:n; %all 4*f(a+nh) terms to f(b) h=(1,3,5,7,9,...,n-1)
x = (a+(i-1));
fx=f*x;
even_func = 4*fx ; %All even function values have a coeffecient of 4
end
for i = 2:3:n ; %all 2*f(a+nh) terms to f(b) h = (2,3,4,6,...,n-2) ;
x = (a+(i-1)) ;
fx = f*x;
odd_func = 2*fx ; %all odd function values have a coeffecient of 2
end
area = outer_func - even_func + odd_func ;
endfunction

채택된 답변

Walter Roberson
Walter Roberson 2016년 4월 7일
"endfunction" is not MATLAB code.
Could you give an example of what the user might enter for the first input?
On the 5th line, where is the x comming from for f(x) =f?
After that line, will f be an array or will it be some kind of function? You treat it as if it is a scalar or array, not as a function.
  댓글 수: 2
Walter Roberson
Walter Roberson 2016년 4월 7일
편집: Walter Roberson 2016년 4월 7일
Do not use input() to get f, interval, num_pts . Pass them on the command line. Pass the f as a function handle. For example,
myfun = @(x) sin( gamma((x.^2+0.0001)) );
simpsonsRule(myfun, [-10, 15], 500)
Then in your code you need to change how you use "f" to recognize that it is a function.
Also I just noticed you have
function area = simpsonsRule = (f, interval, num_pts);
You need to remove the second "="

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by