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

7 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Walter Roberson
Walter Roberson le 7 Avr 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 commentaires
Tshepo Moru
Tshepo Moru le 7 Avr 2016
So how would you suggest I approach it Walter?
Walter Roberson
Walter Roberson le 7 Avr 2016
Modifié(e) : Walter Roberson le 7 Avr 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 "="

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by