Riemann Sum, need help explaining error in my code on my second day of matlab

7 views (last 30 days)
So im trying to do a midpoint sum program for a class, and i am using the functions polevaluu and polint. these are similar to polyvalu and polyint.
so far:
function reimsum(v,a,b,n)
% v is a row vector, the coefficients of a decreasing order polynomial
%a and b are the lower and upper ends and n is # of subintervals
dx = (b-a)/n;
% set value for dx
C = zeros(1,n);
% skeleton for resulting values to be injected
for i=1:n ;
C(i)=(polevaluu(v,(a+(2*i-1))*dx))*2*dx
%the first element in C is equal to polevaluu(v, a+dx) times 2dx
% the second is equal to polevaluu(v, a+3dx) times 2dx
%each element is the area of a rectangle of the sum.
end
polevaluu(polint(v),b)-polevaluu(polint(v),a)
%exact value of integral
Sum(C)
%summation of areas of rectangles.
end
I am getting this error:
Error in ==> reimsum at 5 C(i)=(polevaluu(v,(a+(2*i-1))*dx))*2*dx
not sure exactly what it means, it's my second day using matlab.
Help anyone?
  6 Comments
Walter Roberson
Walter Roberson on 20 Feb 2013
polevaluu is not a built-in MATLAB function. We would need to see the code for it.
Campos
Campos on 20 Feb 2013
if true
function polevaluu(A,x)
%used 2 u's because my MATLAB folder already had a function names
%'polevalu'
n = length(A);
% find length of the polynomial
for i = 0:n-1 ;
A(n-i) = A(n-i)*x^i ;
end
%uses the nth minus i term and multiplies it by the scalar to the ith power
value = sum(A)
%prints the value of the sum.
end
end
should be the same as polyval

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Feb 2013
You are attempting to use polevaluu as a function that returns a value, but your line
function polevaluu(A,x)
is defined to mean that polevaluu does not return any value.
Possibly what you want is
function value = polevaluu(A,x)
  3 Comments
Campos
Campos on 20 Feb 2013
It worked! Thanks!
polint was also having that problem.
now however my reimsum function is returning
the values of every step, so every polevaluu and every poleint...
Walter Roberson
Walter Roberson on 20 Feb 2013
Your code for polevaluu is designed to "prints the value of the sum.". You probably do not want that. Change your line
value = sum(A)
to
value = sum(A);
notice the semi-colon

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!