Double "for loop" with an integral of hundle functions, problem.

>> %assing two handle functions to a column with two rows
f1=@(t)3*t+1;
f2=@(t)1.5*t+0.5;
fcell=cell(2,1);
fcell{1}=f1;
fcell{2}=f2;
>> % gives me a 2x1 cell array, with @(t)3*t+1 in the first row
>> % and @(t)1.5*t+0.5 in the second row.
>> % I will use this cell array in a double "for loop" to produce something
>> C=sym(zeros(2,1));
for kk=1:2
for jj=1:2
C(kk)=C(kk)+integral(@(t)fcell{kk}(t),0,1);
end
end
>> % This, gives me 2 resaults, in a 2x1 sym. The first resault is :
>> C(1)
ans =
5
>> % and the second is :
>> C(2)
ans =
5/2
>> % My problem is that, actually, i have more than 10 handle functions (lets say 10), and
>> % i do not want to write each one, every time. I want to copy them (from another software) and
>> % past in an array (in Matlab). And then, use this array as a source to call these 10 functions in the
>> % double "for loop".
>> % But i cannot make an array of 10 cells, including the 10 hundle functions. They comes
>> % out as "sym". And this, couse problem when i use it in the double "for loop". It gives me
>> % errors like : ........The following error occurred converting from sym to double:
% Error using symengine (line 59)
% DOUBLE cannot convert the input expression into a double array.
% If the input expression contains a symbolic variable, use VPA.
% Any help please

 Accepted Answer

f=@(t)[-7*t+25 %insert (by copy and paste), in the [ ], 10 functions. (Copy %from another software)
140*t+-122
107*t+-56
385*t+-890
-546*t+2834
-72*t+464
-16*t+128
440*t+-3064
-256*t+2504
-142*t+1478
];
result = integral(f,0,1,'ArrayValued',1);
C=zeros(2,1);
for kk=1:2
for jj=1:2
C(kk)=C(kk)+result(kk);
end
end
C
C = 2×1
43.0000 -104.0000

4 Comments

Your answer, solve my question, and i have to accept it, as i will do.
But, as i told to Star Strider, i am thinking make my code more complicated, and i want, again help. If you have, time..
Here is what i want to add :
  1. Like before, insert from another software 10 functions in an array f (to use them later in an integral, inside a double "for loop".
  2. The integration of each of the 10 functions, want to be in different limits, follows by rule ,were d=number of functions in f, and kk=1:d
  3. After i receive an array in which there will be the integrals of each 10 functions (to different limits, each one), lets say this array is
ArrayOf_Int
I want to use the "for loop" as like this (i am adding a product factor to each of the integrals) :
>> for kk=1:d
for jj=1:p
C(kk)=C(kk)+ArrayOf_Int(kk) * 2^jj;
end
end
here p can be any number i want, lets say 50.
Thanks for your help.
f={@(t)-7*t+25 %insert (by copy and paste), in the [ ], 10 functions. (Copy %from another software)
@(t)140*t-122
@(t)107*t-56
@(t)385*t-890
@(t)-546*t+2834
@(t)-72*t+464
@(t)-16*t+128
@(t)440*t-3064
@(t)-256*t+2504
@(t)-142*t+1478
};
d = size(f,1);
lb = (0:d-1).'/d;
ub = (1:d).'/d;
ArrayOf_Int = zeros(d,1);
for i = 1:d
ArrayOf_Int(i) = integral(f{i},lb(i),ub(i));
end
ArrayOf_Int
ArrayOf_Int = 10×1
2.4650 -10.1000 -2.9250 -75.5250 258.8300 42.4400 11.7600 -273.4000 228.6400 134.3100
f={@(t)-7*t+25 %insert (by copy and paste), in the [ ], 10 functions. (Copy %from another software)
@(t)140*t-122
@(t)107*t-56
@(t)385*t-890
@(t)-546*t+2834
@(t)-72*t+464
@(t)-16*t+128
@(t)440*t-3064
@(t)-256*t+2504
@(t)-142*t+1478
};
d = size(f,1);
lb = (0:d-1).'/d;
ub = (1:d).'/d;
ArrayOf_Int = zeros(d,1);
for i = 1:d
ArrayOf_Int(i) = integral(f{i},lb(i),ub(i));
end
C = 1.0;
jjfirst = 1;
jjlast = d;
expression = C*(4^(jjlast+1)-4^jjfirst)/3*sum(ArrayOf_Int)
expression = 4.4249e+08

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 15 Jun 2022

Commented:

on 18 Jun 2022

Community Treasure Hunt

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

Start Hunting!