Trying to finish code for numerical integration on matlab but keep getting error

Hi, I've been trying to do this code on matlab but keep getting the error: Attempted to access fx(1.5); index must be a positive integer or logical.
Error in Untitled (line 30) mpr = mpr+fx((2*i+1)/2)*h; %midpoint rule
I'm not sure if I'm being just another ditsy blonde aha but I don't know how to fix my problem. I have written my code so far at the bottom. I'd love if you could help me!
% Implentation of Rectangle, Trapezium and Simpson's Rule % a: lower bound, b: upper bound, N: number of strips
clear, clf,clc
fprintf(' \n Numerical Integration');
fprintf(' \n Input values for integral boundaries a and b with a<b')
fprintf(' \n a = '); a= input('');
fprintf(' \n b = '); b= input('');
if a > b;
fprintf(' \n a has to be less than the value of b')
fprintf(' \n Input new values for a and b')
fprintf(' \n a = '); a= input('');
fprintf(' \n b = '); b= input('');
end
fprintf(' \n Input the number of strips')
fprintf(' \n N = '); N= input('');
fx = input('type in f(x)=');
R = input('give the range a,b=');
x = linspace(R(1), R(2),100);
eval(fx)
h = (b-a)/N;
mpr = 0;
tr = 0;
for i = 1:N;
mpr = mpr+fx((2*i+1)/2)*h; %midpoint rule
tr = tr+(fx(i)+fx(i+1))/2*h; %trapazoidal rule
end

Answers (2)

hi Sarah, First there is a problem with the input, you have to define the range x then the function input, R represents the bounds then write R as function of (a,b) and you did not use the variable N. Here is the edited code i think it is structurally correct :
clear,
fprintf(' \n Numerical Integration');
fprintf(' \n Input values for integral boundaries a and b with a<b')
fprintf(' \n a = '); a= input('');
fprintf(' \n b = '); b= input('');
if a > b;
fprintf(' \n a has to be less than the value of b')
fprintf(' \n Input new values for a and b')
fprintf(' \n a = '); a= input('');
fprintf(' \n b = '); b= input('');
end
fprintf(' \n Input the number of strips')
fprintf(' \n N = '); N= input('');
R(1)=a;R(2)=b;
x = linspace(R(1), R(2),N);
fx = input('type in f(x)=');
h = (b-a)/N;
mpr = 0;
tr = 0;
for i = 1:(N-1);
mpr = mpr+fx(floor(i))*h; %midpoint rule
tr = tr+(fx(i)+fx(i+1))/2*h; %trapazoidal rule
end
Verify with a=0, b=pi/2,N=1000, you should get tr=mpr=0.99.

5 Comments

hello thanks for posting youssef, I tried your code but i got an error saying Attempted to access fx(7); index out of bounds because numel(fx)=6.
Error in Numerical_Integration (line 29) tr = tr+(fx(i)+fx(i+1))/2*h; %trapazoidal rule
i wondered if you knew how to solve this problem?
hi, i cant explain , can you post the parameters you used please?
This is because your fx is ending up a vector, not a function.
hi, yes i wrote a the script for vector not for function handle and it is working ..
ahh I see, thank you for clearing this up. Do either of you know how to write fx as a function instead? for the project the user has to be able to input any function of fx themselves

Sign in to comment.

You define x, then you use input() to ask the user for the function. The form of input() you used will execute the function you typed in. That is going to have the effect of evaluating the function at values that are in x, resulting in a vector being stored in fx. After that your attempts to code fx() are going to be acting as array indices rather than as function calls.
You should look at the 's' option of input() and you should be looking at str2func()

4 Comments

hello walter i was wondering if you could elaborate on the 's' option with maybe an example of it in use? sorry i haven't been using matlab for very long
I had a look at this and I was still confused as to what to write for my own code. This is why I had reposted the question asking for help with the function. I am quite useless at matlab and need a few clearer examples than on the page to help my understanding. Sorrryyyy
f = input('What do you want to integrate?', 's');
fx = @(X) feval(f, X);
Now you can use fx the way you wrote it above, such as fx((2*i+1)/2)
I think, though, you will have more luck if you were to use fx((x(i)+x(i+1))/2)

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 17 Apr 2014

Commented:

on 20 Apr 2014

Community Treasure Hunt

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

Start Hunting!