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

7 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Apr. 2016
"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 Kommentare
Walter Roberson
Walter Roberson am 7 Apr. 2016
Bearbeitet: Walter Roberson am 7 Apr. 2016
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 "="

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by