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 件のコメント
Tshepo Moru
Tshepo Moru 2016 年 4 月 7 日
So how would you suggest I approach it Walter?
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 Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by