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

3 visualizaciones (últimos 30 días)
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

Respuestas (2)

Youssef  Khmou
Youssef Khmou el 17 de Abr. de 2014
Editada: Youssef Khmou el 17 de Abr. de 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 comentarios
Youssef  Khmou
Youssef Khmou el 18 de Abr. de 2014
hi, yes i wrote a the script for vector not for function handle and it is working ..
beth
beth el 20 de Abr. de 2014
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

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 17 de Abr. de 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 comentarios
beth
beth el 20 de Abr. de 2014
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
Walter Roberson
Walter Roberson el 20 de Abr. de 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)

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by