Integrating individual matrix values using a loop

5 visualizzazioni (ultimi 30 giorni)
jackoboy9
jackoboy9 il 7 Giu 2016
Commentato: José-Luis il 7 Giu 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 Commento
José-Luis
José-Luis il 7 Giu 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.

Accedi per commentare.

Risposte (1)

José-Luis
José-Luis il 7 Giu 2016
Modificato: José-Luis il 7 Giu 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 Commenti
jackoboy9
jackoboy9 il 7 Giu 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 il 7 Giu 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,

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by