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