I'm having problems understanding how to use trapz. Lets say I have this code:
quad(@(x)myfun,-2,2)
Were myfun is a function in a different .m-file. But I want to solve it using trapz instead of quad. With quad its simple. You just write your function and then the limits of the integral. But with trapz I'm not at all sure how to do even when having read the help about it several times.
Another question on the same topic is about cumtrapz. I wonder what the difference between trapz and cumtrapz are and when to use which?
EDIT:
Let me specify some further questions. First in trapz(X,Y) what doest X and Y signify? Is Y the function that trapz solves? If it is so what does X do? My problem is bigger than one small integral and to better understand how to solve it I will show a more complicated example which is part of what I have to do. I got a function that looks like this:
function Wiz = Wizfun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q,z,sigma)G = G2.*(1+((T-300)./TM).*TMO);Ei = -(1/2.*pi).*((1+ny)./(1-ny)).*G.*A.*Epsi;Wiz = -(2/3).*b.*x.*Ei.*(1./(x.^2+z.^2));
I know I'm not using all the in-data in the function but I have many functions who calls each other and some of them uses all those parameters. In my main window I then write like this:
syms yT = 1273;TM = 2163;TMO = -0.5;G2 = 126000;A = 1.2E-29;b = 0.000000000258;x = 2/3*b;Epsi = 0.00487903650119246;ny = 0.3;Vc = 1E-9;c2 = 18.07551;D2 = 0.0000169;Q = 263900;syms z;sigma = 170;intg = quad(@(z) Wizfun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q,z,sigma),-250*b,250*b)
And I get this answer:
intg =1.3118e-034
If I write trapz as Matt suggest:
Edit 2, changing from x = -2:.01:2; to the right numbers:
x = -250*b:0.1*b:250*b;intg = trapz(x,Wizfun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q,z,sigma))
I get this answer:
intg =0
Why does this value differ from quad? Is it because the quad value is too small? If I write like Jan suggest:
X = linspace(-2, 2, 1000)Y = Wizfun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q,z,sigma); % Or a loop, if myfun cannot handle vectors
Int = trapz(X, Y)
I get this error:
??? Undefined function or method 'max' for input arguments of type 'sym'.Error in ==> trapz at 43perm = [dim:max(ndims(y),dim) 1:dim-1];Error in ==> test at 30Int = trapz(X, Y)
This might be because of that loop thing you wrote about but I'm not sure what you meant by that and how to do a loop.
Best Answer