how can i use integral function with vector limits by using another for loop ?

10 vues (au cours des 30 derniers jours)
work wolf
work wolf le 3 Juil 2023
Commenté : work wolf le 5 Juil 2023
% 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' ?

Réponses (1)

Star Strider
Star Strider le 3 Juil 2023
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)
y = 1×3
-0.2075 1.7554 -0.0391
.
  17 commentaires
Torsten
Torsten le 5 Juil 2023
I thought I was clear enough that recursion only works with a loop.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by