Borrar filtros
Borrar filtros

Keeping a variable when using trapz?

2 visualizaciones (últimos 30 días)
Rachel
Rachel el 23 de En. de 2013
I'm writing a program which integrates and evaluates Gauss's Law using trapz. One of the shapes I need to integrate is a disk. I'm trying to do that by integrating the electric field of a ring over the radius of the disk (like here).
The problem is that the function is dependent on z, which can't be separated along with the other constants. I want to leave the z dependence in the function, anyway, so that later I can create an array of values of the electric field along the z-axis to graph. When I try to run my program, this one function errors, saying: ORDER contains an invalid permutation index.
Here is what I have:
rdisk = 2.5;
radius = 0:rdisk/100:rdisk;
radialfunction = radius/(z^2+radius.^2).^(3/2);
Edisk = 1:500;
for z=0.01:0.01:5
radial = trapz(radius, radialfunction);
Edisk(i) = ((-sigma*z)/(2*epsilon))*radial;
i=i+1;
end
Any thoughts as to how I could work around this bug would be greatly appreciated. Thank you!

Respuestas (1)

Walter Roberson
Walter Roberson el 23 de En. de 2013
You define
radialfunction = radius/(z^2+radius.^2).^(3/2);
but "z" has no obvious definition at that point.
Whatever definition it does has will be evaluated and "locked in" to the value of radialfunction.
Are you expecting z to be a sym ? If you are then please note that if you have
syms z
f = z
z = 5
f
then you will find that f is still "z" and does not get evaluated according to the current value of z. You need to subs() to get the current value of z to replace the symbol "z", as in
subs(radialfunction)
Note, though, that that would result in symbolic numbers (at least in theory; Azzi showed a counter-example the other day), so if you want the result to be numeric you should double() the result,
double(subs(radialfunction))
Please be careful in your definition of radialfunction about whether you want matrix division (the / operator) or you want element-by-element division (the ./ operator)

Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by