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

조회 수: 9 (최근 30일)
beth
beth 2014년 4월 17일
댓글: Walter Roberson 2014년 4월 20일
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

답변 (2개)

Youssef  Khmou
Youssef Khmou 2014년 4월 17일
편집: Youssef Khmou 2014년 4월 17일
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
Youssef  Khmou
Youssef Khmou 2014년 4월 18일
hi, yes i wrote a the script for vector not for function handle and it is working ..
beth
beth 2014년 4월 20일
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

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2014년 4월 17일
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
beth
beth 2014년 4월 20일
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 2014년 4월 20일
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)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by