Solve integrals with Matlab

33 ビュー (過去 30 日間)
Jerald Johnson
Jerald Johnson 2019 年 4 月 22 日
回答済み: Sara 2024 年 3 月 19 日
Hey guys i am working on a calculus problem and it requires me to use analytical solution, trapezoidal numerical integration, simpson's rule, lobatto quadrature and global adaptive quadrature. I keep getting errors and i have no idea what i am doing incorrect. Could someone help me write the code for this? Thanks.
Problem: f(x)= integral sign(5+1/sqrt(1-x^2)). Upper bound of integral sign is 1/2 and lower bound of integral sign is 0.
% Integrating using different commands
x=0:0.1:1/2;
y=(5+1/sqrt(1-x^2));
format long
% Analytical Solution
A0=(5+1/sqrt(1-x^2))
% Trapezoidal Numerical Integration
A1= trapz(x,y)
% Simpson's Rule
A2= quad('sqrt(1-x^2'x(1),x(end))
% Lobatto Quadrature
A3=quadl('sqrt(1-x^2'x(1),x(end))
% Global Adaptive Quadrature
A4=integral(intfun,x(1),x(end))

採用された回答

David Wilson
David Wilson 2019 年 4 月 22 日
編集済み: David Wilson 2019 年 4 月 22 日
Do you have the symbolic toolbox?
If so, start with the int command. You can do both indefinite and definite integration.
syms x real
f = 5 + 1.0./sqrt(1-x^2) % dot-divide not strictly needed here
Q = int(f,x) % indefinite
Q = int(f,x,0,1/2)
Now that we have an anlytical answer, , we can validate numerical schemes.
First we should make a vectorised anonymous function of the integrand,
f = @(x) 5 + 1.0./sqrt(1-x.^2)
Matlab has pre-canned routines for the final two integration schemes:
A3 =quadl(f,0,0.5)
A4=integral(f,0,0.5) % Global Adaptive Quadrature
For the (composite) trapz and Simpson's you need to decide on a step size (as you did above).
h = 0.1; % step size (guess)
x=0:h:1/2;
A1= trapz(x, f(x))
A quick hack of the Simpson's rule is
n = 10; % must be even
a = 0; b = 0.5; % integration limits
x = linspace(a,b,n+1)';
c = [1,repmat([4 2],1,n/2-1),4,1];
A2 = (b-a)*c*f(x)/n/3; %
It might be prudent to check all numerical values with the analytical one, especially the Simpson's implementation above.

その他の回答 (2 件)

mohamed adel
mohamed adel 2020 年 5 月 26 日
∫_3^5▒〖1/√(x^2-4) dx〗

Sara
Sara 2024 年 3 月 19 日
P(a,b)=1/√2π ∫_a^b▒〖e^((-x^2)/2) dx〗

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by