Trapezium rule

  • Follow


Hi, I'm a complete noob to matlab and am trying to write a program for the trapezium rule for (x^2+2*x)*sin(x), but I keep getting the error message:

??? Error: File: trap2.m Line: 16 Column: 68
Unbalanced or unexpected parenthesis or bracket.
 
Here is my program:

% trapeziumrule.m test program for numerical integration using the composite
% trapezium rule to solve the integral of (x^2+2*x)*sin(x) between a and b
clear;
format long; 
% So matlab can use more decimal places
a=input('input a (Please Write an end value ->');
b=input('input b (Please Write an end value) ->');
n=input('The number of intervals (n) ->');
%I have used this function so we can input the values for a, b and n in the
%command window once we have ran the trapeziumrule.m file.
h=(b-a)/n;
fa=(a^2+2*a)*sin(a); % f(a)
fb=(b^2+2*b)*sin(b); %f(b)
ff=0;
for i=2:n
ff=ff+(2*(((a+(h*(i-1))))^2+2*((a+(h*(i-1)))))*sin(((a+(h*(i-1))))); % sum of 2f(a+i(h)) where i=1 to n-1
end
result=(h/2)*(fa+fb+ff);
% result=f(a)+f(b)+ sum of 2f(a+i(h)) where i=1 to n-1

could someone help me with where I am going wrong please?

Many thanks.
0
Reply Sarah 12/8/2010 10:00:23 PM

On 10-12-08 04:00 PM, Sarah wrote:

> ff=ff+(2*(((a+(h*(i-1))))^2+2*((a+(h*(i-1)))))*sin(((a+(h*(i-1))))); %

You have a '(' before the '2*' but that bracket is not closed anywhere. How 
far did you want that expression to extend? If you want it to extend to the 
end of line, then as all of the operations at the same level from that point 
on are multiplications, the '(' before the '2*' would be redundant; the same 
if you want it to close just before the '*sin'. Without disturbing the flow of 
the subexpressions, at the moment I cannot see any place to put the ')' that 
would not make the '(' ')' pair redundant.
0
Reply Walter 12/8/2010 10:37:28 PM


1 Replies
419 Views

(page loaded in 0.026 seconds)

Similiar Articles:













7/26/2012 8:17:34 AM


Reply: