how can i use integral function with vector limits by using another for loop ?
Show older comments
% Define the function for integration
f = @(t, y,m) y + (t-m);
% Create sample data
m=[0 0.2 0.3 0.4 ];
datalower = [0.1 0.5 0.6];
dataupper = [0.9 2 0.5];
% Define the integral function handle
IntV = @(y,k,m, lower, upper) integral(@(t) f(t, y(k),m(k)), lower(k), upper(k));
% To use it within a a recursive formula without using for loop as follows:
k = 1:numel(m)-1;
y(1)=0;
y(k+1) = y(k) + IntV(y,k,m, lower, upper)./m(k-2) ;
by looking for integral function, it's not accept vector limitis, Thus can i convert integral to matrix or summations ( with respect to same result) to aviod using for loop or symbolic 'int' ?
Answers (1)
All the vectors have to be the same size ()so I shortened ‘m’ here), then arrayfun works (and so would a loop, indexing each vector) —
% Define the function for integration
f = @(t, y,m) y + (t-m);
% Create sample data
m=[0 0.2 0.3];
datalower = [0.1 0.5 0.6];
dataupper = [0.9 2 0.5];
% Define the integral function handle
IntV = @(y,m, lower, upper) integral(@(t) f(t, y,m), lower, upper);
% To use it within a a recursive formula without using for loop as follows:
% k = 1:numel(m)-1;
% y(1)=0;
% y(k+1) = y(k) + IntV(y,k,m, lower, upper)./m(k-2) ;
y = randn(size(m));
y = arrayfun(IntV,y,m,datalower,dataupper)
.
17 Comments
I don't know how you want to divide by m(k-2) since you will reference m(-1) and m(0) which do not exist. But maybe something like this ?
% Define the function for integration
f = @(t,y,m) y + (t-m);
% Create sample data
m=[0 0.2 0.3 0.4 ];
datalower = [0.1 0.5 0.6];
dataupper = [0.9 2 0.5];
% Define the integral function handle
IntV = @(y,m,lower, upper) integral(@(t) f(t,y,m), lower, upper);
y(1) = 0;
for k = 1:numel(m)-1
y(k+1) = y(k) + IntV(y(k),m(k),datalower(k),dataupper(k));
end
y
My pleasure!
I cannot help because what you want to do does not make sense.
As in my code, ‘y’ must be the same size as the other elements.
% Define the function for integration
f = @(t, y,m) y + (t-m);
% Create sample data
m=[0 0.2 0.3];
datalower = [0.1 0.5 0.6];
dataupper = [0.9 2 0.5];
% Define the integral function handle
IntV = @(y,m, lower, upper) integral(@(t) f(t, y,m), lower, upper);
% Define the function handle dep. on y to use in a recursive formula by using arrayfun
intV_Fun = @(y) arrayfun(IntV,y,m,datalower,dataupper); %y = datalower; %randn(size(m)); test
%-------------------------------------------------------------------------------------------------
% IntV_Fun = @(y) splitapply(@(k) IntV(y(k),m(k),datalower(k),dataupper(k)), k, k);
% IntV_Fun = @(y) cell2mat(arrayfun(@(k) IntV(y(k),m(k),datalower(k),dataupper(k)), k,'UniformOutput', false) );
% IntV_Fun = @(y) accumarray(k.', k, [], @(k) IntV(y(k),m(k),datalower(k),dataupper(k) ) );
% IntV_Fun = @(y) groupsummary(k, k, @(k) IntV(y(k),m(k),datalower(k),dataupper(k) ) );
% IntV_Fun = @(y) groupsummary(k', k', @(k) IntV(y(k),m(k),datalower(k),dataupper(k) ) );
% IntV_Fun = @(y) groupsummary({y,m,datalower,dataupper}, k,...
% @(y,m,datalower,dataupper) IntV(y,m,datalower,dataupper) ) ;
% IntV_Fun = @(y) groupsummary({y' , m', datalower', dataupper'}, k',...
% @(y,m,datalower,dataupper) IntV(y,m,datalower,dataupper ) );
%
% all gives 1x3 vector when y know for example y =y = datalower; (!)
%---------------------------------------------------------------------------------------------------------------------------
% To use it within a a recursive formula without using for loop as follows:
% I can make this work (sort of) however nothing further —
k = 3;
y(3) = 0;
intV_Fun(y(k)*ones(size(m)))./m(k-2)
% NOTE — It expands the scalar value for 'y' to a vector to match the sizes of the rest
% of the arguments.
% This is not recursive because there is no recursion of any kind —
k = 1:numel(m)-1; % k = 1:numel(m); same Error
y(1)=0;
y(k+1) = y(k) + intV_Fun(y(k)*ones(size(m)))./m(k-2) ;
% gives same Error in all cases, i.e, splitapply, accumarray & groupsummary
% Error : Index exceeds the number of array elements. Index must not exceed 1.
Describe what you want to do and it may be possible to do it (no promises). I cannot figure it out from the code.
.
work wolf
on 4 Jul 2023
work wolf
on 4 Jul 2023
Star Strider
on 4 Jul 2023
I am lost. We now have ‘y1(k)’ through ‘y4(k)’ which seems to be diverging from anything reasonable.
What do you want to do? What problem do you want to solve? What is the objective?
work wolf
on 4 Jul 2023
Star Strider
on 4 Jul 2023
There is a limit to what you can do efficiently with vectorisation, and sometimes it is indeed preferable. For some problems however, loops are more efficient.
If a loop works (and seems to make the problem easier, as it does here), just use a loop.
Vectorisation is not actually ‘loop-free’. In most instances, the loops are there (as they muxt be), simply not visible, and hidden in the vectorisation code.
.
Star Strider
on 4 Jul 2023
Not every operation can be efficiently vectorised. Sometimes, a loop is more efficient. Creating complicated vectorised code may be less efficient that just using a loop.
In any event, I do not understand what you want to do. If you describe what you want to do, and the problem you want to solve, I may be able to help.
Recursive systems are not suited to be vectorized. And to be honest: I don't know how it could be achieved for your case. A simple for-loop is easy to understand and adequate here.
Jan's answer might be of interest for you:
And it's no problem to vectorize these steps
1. 1/2 integral(f, m(1), m(2)) when k == 1
2. 1/3 integral(f, m(k-1), m(k+1)) when k = 2:numel(m)-1
3. 1/2 integral(f, m(end-1), m(end)) when k = numel(m)
but according to what you wrote before, f changes with k - and that's the problem.
work wolf
on 5 Jul 2023
You didn't write anything about a recursion - "array" is just made up of the numel(m) terms in your list:
f = @(x)x.^2;
m = 0:10;
array = [1/2*integral(f,m(1),m(2)),1/3*arrayfun(@(mkm1,mkp1)integral(f,mkm1,mkp1),1:numel(m)-2,3:numel(m)),1/2*integral(f,m(end-1),m(end))]
work wolf
on 5 Jul 2023
Torsten
on 5 Jul 2023
I thought I was clear enough that recursion only works with a loop.
work wolf
on 5 Jul 2023
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!