solve some integrals using trapezoidal rule and a matlab built-in function and to represent the original function in a graph

7 次查看(过去 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