Is it possible to creare a matrix of functions?

136 Ansichten (letzte 30 Tage)
Riccardo Bartoloni
Riccardo Bartoloni am 1 Mai 2019
Bearbeitet: Guillaume am 2 Mai 2019
IMG-20190501-WA0001 (1).jpeg
Hello, i am working on a code for my university. Is it possible to create a vector of 229 functions in a way that if i select an element, for exemple tc(3), as in the image, i can work on the 3st function of my vector. Thank you very much!
  10 Kommentare
Riccardo Bartoloni
Riccardo Bartoloni am 2 Mai 2019
Thank you very much Stephen Cobeldick! Thanks to your suggestions, using vectorization, i was able to delete a lot of double "for" loop, and now my code is more or less 20 times faster.....Sorry but i am a beginner at using Matlab... :D :D
I paste my new code. My fundamental question is: how can i do to find the optimal parameters (vector Param) that minimize SSE (in my code called "SQ_TOT"), without knowing these parameters?
clc
clear
t=0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333:0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333:76.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333;
Param=[0.5254010 11.4402640 29861.1830810 2.0010250 -0.051606000 -0.057373000 0.036517000 -0.099394000 1.647586000 81.195773000 1.422753000 -0.078617000 0.036363000 0.106596000 0.128553000]
load('DUM.mat'); %4x229
DUMqA=[Param(5:8)];
expbeta=exp(DUMqA*DUM);
t1=t.^Param(4);
t2=t.^Param(4);
t2(length(t))=[];
t2=[0 t2];
tc=t1-t2;
lt=length(t);
cohort=zeros(1,lt);
for i=1:length(t1);
summ=[zeros(1,i-1) expbeta(i:lt).*tc(1:lt+1-i)];
cohort=[cohort;summ];
end;
cohort(1,:)=[];
HEY_A=cumsum(cohort,2);
F_A=(1-(Param(3)./(Param(3)+HEY_A)).^Param(2)).*(1-Param(1));
%%%%Retention Model%%%%%
DUMqR=[Param(12:15)];
expbeta_R=exp(DUMqR*DUM);
t1R=t.^Param(11);
t2R=t.^Param(11);
t2R(length(t))=[];
t2R=[0 t2R];
tc_R=t1R-t2R;
cohort_R=[];
%cohort_R= matrice riga 1000
for i=1:length(t1);
summ=[zeros(1,i-1) expbeta_R(i:length(t1)).*tc_R(1:length(t)+1-i)];
cohort_R=[cohort_R;summ];
end;
cohort_R(1,:)=[];
HEY_R=cumsum(cohort_R,2);
F_R=(1-(Param(10)./(Param(10)+HEY_R)).^Param(9));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Addition%%%%
load('housed.mat'); %1x229
HouseCha1=housed;
%HouseCha1= riga 19
housed(229)=[];
HouseCha2=[0 housed];
ChaninPop=HouseCha1-HouseCha2;
F_AZ=[zeros(lt,1) F_A];
EXP_A=[];
EXP_AA=[];
EC_RVERO=zeros(1,lt);
EC=[];
%EC= riga 976 oppure colonna IA e riga 1712
F_RZ=[zeros(228,1) F_R];
F_AZ(:,end)=[];
F_RZ(:,end)=[];
ChaninPopR=ChaninPop(1:end-1);
EC_RVERO=[0];
for i=1:lt
EXP_AA=(F_A-F_AZ).*(ChaninPop+EC_RVERO)';
EC=sum(EXP_AA);
EC(lt)=[];
EXP_RR=(F_R-F_RZ).*(EC)';
EC_RVERO=sum(EXP_RR);
EC_RVERO=[0 EC_RVERO];
EC_RVERO(end)=[];
end
EC=[EC sum(EXP_AA(:,end))];
EC_RVERO(1)=[];
EC_RVERO=[EC_RVERO sum(EXP_RR(:,end))];
EC3m_R=[0 0];
for i=3:lt;
summ=sum(EC_RVERO(i-2:i));
EC3m_R=[EC3m_R summ];
end;
EC3m=[EC(1) sum(EC(1:2))];
for i=3:lt;
summ=sum(EC(i-2:i));
EC3m=[EC3m summ];
end;
load('DishADD.mat'); %1x229
DIV_=ADD~=0;
DIVEC3=DIV_.*EC3m;
DIV_A=(DIVEC3-ADD);
DIV_A2=(DIV_A).^2;
SQ_ADD=sum(DIV_A2(1,:));
load('LOSS.mat'); %1x229
DIV=LOSS~=0;
DIV_L=DIV.*EC3m_R;
DIV_LOSS=(DIV_L-LOSS)
DIV_LOSS2=(DIV_LOSS).^2
SQ_LOSS=sum(DIV_LOSS2(1,:));
EXP_E=EC-EC_RVERO;
EXP_END=[];
for i=1:lt;
summ=sum(EXP_E(1:i));
EXP_END=[EXP_END summ];
end;
load('END.mat');
DIV1=END~=0;
DIV2=DIV1-DIV_;
DIV_E=DIV2.*EXP_END;
END_DIV=DIV2.*END;
DIV_E(1)=EXP_END(1)
SQ_EN=[(EXP_END(1))^2];
pos=find(DIV2==1);
for i=4:3:pos(length(pos));
DIV_END=((DIV_E(i)-DIV_E(i-3))-(END_DIV(i)-END_DIV(i-3)))^2;
SQ_EN=[SQ_EN DIV_END];
end;
SQ_END=sum(SQ_EN(1,:));
SQ_END=SQ_END+(END(length(END))-EXP_END(length(EXP_END)))^2;
SQ_TOT= SQ_LOSS+SQ_END+SQ_ADD
Guillaume
Guillaume am 2 Mai 2019
Bearbeitet: Guillaume am 2 Mai 2019
Note that there's never any point(*) to write numbers with that many digits:
t=0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333:0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333:76.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333;
A number of class double can only store about 16 different digits and matlab will ignore the extra digits.
t = 0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333
%and
t = 0.3333333333333333
%and the simpler
t = 1/3
will all result in the exact same value stored in memory (which if decoded in decimal by James Tursa's excellent num2strexact is 0.333333333333333314829616256247390992939472198486328125, the best approximation to 1/3 you can get with double)
The best way to write your expression is thus:
t = 1/3 : 1/3 : 76+1/3;
------
* others than for messing up the formatting of activity feed on this forum, it turns out!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John D'Errico
John D'Errico am 1 Mai 2019
Bearbeitet: John D'Errico am 1 Mai 2019
Do you want to create function handles? For example,
fmat = {@(x) sin(x), @(x) cos(x), @(x) exp(x), @(x) x^2 + 1};
fmat{1}(2)
ans =
0.9093
fi = fmat{2}
fi =
function_handle with value:
@(x)cos(x)
Or you could create an array of symbolic objects.
syms x
fmat = [x, x^2+2, (x+1) -2*x];
then you could use tham as functions too.
matlabFunction(fmat(1))
ans =
function_handle with value:
@(x)x
A virtue of an array of symbolic objects is you can do many things with them all.
int(fmat,x)
ans =
[ x^2/2, (x*(x^2 + 6))/3, (x*(x + 2))/2, -x^2]
And you can even use them all at once, in one call.
fall = matlabFunction(fmat)
fall =
function_handle with value:
@(x)[x,x.^2+2.0,x+1.0,x.*-2.0]
>> fall(3)
ans =
3 11 4 -6
  4 Kommentare
Riccardo Bartoloni
Riccardo Bartoloni am 1 Mai 2019
But in my case i don't know the value of vector x. Because at the end i have to find the optimal vector x that minimize SSE. Maybe looking at this post it should be clearest https://it.mathworks.com/matlabcentral/answers/459500-how-can-i-find-the-optimal-parameters-that-minimize-sse?s_tid=prof_contriblnk
Riccardo Bartoloni
Riccardo Bartoloni am 1 Mai 2019
I am arrived here. How i have to do if i want to create a new function that is the multiplication between the second function of expbeta (expbeta{2}) and the first function of tc (tc{1})?
clc
clear
t=0.33333: 0.333333: 76.333333; %length(t)=229
load('DUM.mat'); %Matrice 4x229
t2=t;
t2(length(t2))=[ ]
t2=[0 t2];
lt=length(t);
for i=1:lt;
summ= @(x) t(i)^x(4)-t2(i)^x(4);
tc{i}=summ; %1x229-1x229=1x229
end
for i=1:lt;
summ=@(x)exp(x(5:8)*DUM(1:4,i));
expbeta{i}=summ;
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by