Integrating individual matrix values using a loop

5 次查看(过去 30 天)
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 个评论
José-Luis
José-Luis 2016-6-7
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.

请先登录,再进行评论。

回答(1 个)

José-Luis
José-Luis 2016-6-7
编辑:José-Luis 2016-6-7
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 个评论
jackoboy9
jackoboy9 2016-6-7
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 2016-6-7
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,

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by