필터 지우기
필터 지우기

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.
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
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
Walter Roberson 2015년 5월 10일
Vectorize your division, not just your multiplication.
f1=@(x) (x.^2+1)./((x+2).*sqrt(1-2*x));

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by