フィルターのクリア

Using multiple integration methods in one script

1 回表示 (過去 30 日間)
Kyle Donk
Kyle Donk 2020 年 2 月 1 日
回答済み: David Hill 2020 年 2 月 1 日
Hi, I have to calculate an integral using the midpoint, trapezoid, and simpson's methods. I already have the functions stored in one file in Matlab, but I am encountering difficulty when trying to use them all in one script. I have the integral (sqrt(4-x^2)) saved in a function (called Func). These three methods each call Func. I would appreciate any hints or tips that anyone can give me. THIS IS A SCHOOL ASSIGNMENT, SO NO COMPLETE ANSWERS. Here is my script (eventually, N will contain an array of numbers):
%Approximating an Integral using multiple methods
y=0;
sum=0;
a=0;
b=2;
for N=[10]
Mid(a,b,N)
midval=sum
abserror=pi-y
Trap(a,b,N)
trapval=sum
abserror=pi-y
Simp(a,b,N)
simpval=sum
abserror=pi-y
end
fprintf('%s | %s | %s | %s \n','N','Method','Value','AbsError')

回答 (1 件)

David Hill
David Hill 2020 年 2 月 1 日
Func=@(x)sqrt(4-x^2);
Integral=@(x)2*asin(x/2)+x*sqrt(4-x^2)/2;
a=0;
b=2;
N=10;
m=Mid(a,b,N,Func);
t=Trap(a,b,N,Func);
s=Simp(a,b,N,Func);
I would pass Func to your integration functions. You will get answers back (m,t,s) from each of your functions that you can compare to the actual evaluation of the actual integral.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by