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

3 ビュー (過去 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 Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by