solve some integrals using trapezoidal rule and a matlab built-in function and to represent the original function in a graph
조회 수: 1 (최근 30일)
이전 댓글 표시
It is asked to solve some integrals using trapezoidal rule and a matlab built-in function and to represent the original function in a graph.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149980/image.jpeg)
Here is the code i did:
f1=@(x) (x.^2+1)/((x+2).*sqrt(1-2*x));
a=0; b=.5;
N=100;
figure()
fplot(f1,[a,b])
grid on
res_trap=trapezios(f1,a,b,N);
res_matlab=integral(f1,a,b);
f1=@(x)((sin(x))/x.^2);
figure()
fplot(f1,[0,10])
grid on
a=1; b=inf;
N=10;
res_trap=trapezios(f1,a,b,N);
res_matlab=int(f1,1,inf);
syms t
f1=@(x,y)x.^2*y;
a=0; b=y; c=0; d=1;
eixox=@(y)y;
ezsurf(f1,0,eixox(),0,1)
res_trap=trapezios(f2,c,d,N);
res_matlab=int2(f1,0,1,0);
Here is the trapezoidal integration function I did:
function It=trapezios(f,a,b,N)
h=(b-a)/N;
It=0;
for k=1:(N-1)
x=a+h*k;
It=It+feval(f,x);
end
It=h*(f(a)+f(b))/2+h*It;
end
When i run the code several errors occur. What is wrong?
댓글 수: 1
Image Analyst
2015년 5월 10일
You might get answers faster if you told people what the errors were rather than forcing them to copy the code, switch to MATLAB, paste the code, and then run it.
답변 (1개)
Walter Roberson
2015년 5월 10일
Vectorize your division, not just your multiplication.
f1=@(x) (x.^2+1)./((x+2).*sqrt(1-2*x));
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!