Trying to finish code for numerical integration on matlab but keep getting error
2 views (last 30 days)
Show older comments
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)
Youssef Khmou
on 17 Apr 2014
Edited: Youssef Khmou
on 17 Apr 2014
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
Youssef Khmou
on 18 Apr 2014
hi, yes i wrote a the script for vector not for function handle and it is working ..
Walter Roberson
on 17 Apr 2014
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
Walter Roberson
on 20 Apr 2014
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)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!