solve some integrals using trapezoidal rule and a matlab built-in function and to represent the original function in a graph
10 views (last 30 days)
Show older comments
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 Comment
Image Analyst
on 10 May 2015
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.
Answers (1)
Walter Roberson
on 10 May 2015
Vectorize your division, not just your multiplication.
f1=@(x) (x.^2+1)./((x+2).*sqrt(1-2*x));
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!