Integrating individual matrix values using a loop

5 visualizaciones (últimos 30 días)
jackoboy9
jackoboy9 el 7 de Jun. de 2016
Comentada: José-Luis el 7 de Jun. de 2016
Hey,
[vm,tm]=meshgrid(v,t); %make 2d grid of v and t values
[nr,nc]=size(tm);
for ii=1:nr
for jj=1:nc
vm(ii,jj);
tm(ii,jj);
tau=(vm.^2.*tm)./(2.*a);
sigma3=(vm.*R)./(2.*a);
fun=@(tau) exp(-(sigma3).^2./(2.*tau)-tau./2).*tau.^(-3/2);
q=integral(fun,tau,inf); %not working because tau is a matrix
k1=1-(sigma3./(sqrt(2.*pi))).*exp(sigma3).*q;
end
end
What I want the code to do is to output k1 as a matrix of values which I can then plot against tau. The function file asks for input of v, t, a, R. If I input single v and t values I get a single (correct) answer for k1, but if I input values of v and t as a matrix, lets say [1:10] and [1:10], then the code complains and says that A and B must be floating point scalars. This makes sense because one of my limits of the integral is currently the matrix tau rather than the individual value that it used in the previous steps.
How can I fix this?
Thanks.
  1 comentario
José-Luis
José-Luis el 7 de Jun. de 2016
vm(ii,jj);
tm(ii,jj);
tau=(vm.^2.*tm)./(2.*a);
vm(ii,jj) does nothing. Same for tm(ii,jj)
After that you are using the entire vm and tm arrays in each loop iteration, instead of each single element, which I guess is what you want.
Plus you don't really need the loop to be performing element by element operations.

Iniciar sesión para comentar.

Respuestas (1)

José-Luis
José-Luis el 7 de Jun. de 2016
Editada: José-Luis el 7 de Jun. de 2016
I'll give you a hint to get you started. Get rid of the loops and use element by element multiplication:
[vm,tm]=meshgrid(v,t);
tau=(vm.^2.*tm)./(2.*a);
Will work just fine without looping, assuming a is either of the same size as tm and vm or a scalar. I have not idea what a and R are but the principle is the same.
  2 comentarios
jackoboy9
jackoboy9 el 7 de Jun. de 2016
Sorry. Yeah a and R are scalars.
The problem I'm having is when I have to use tau as a lower bound for my definite integral since even when defined as you have done, it still thinks it's a non-scalar entity and produces an error.
José-Luis
José-Luis el 7 de Jun. de 2016
I am not sure I follow. Do you want to perform an integral for every value in the matrix? Because the second argument of the tau function needs to be a scalar. You can not pass a function as you seem to be doing? Do you want to pass the result of that function?
In any case you can not do it in a vectorized manner and would need to loop.
for ii=something
for jj=something
dummy = integral(tau,tau(ii,jj),inf)
end
end
Although why you want to pass the function result as a lower limit for the integral is beyond me.
Cheers,

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by