Out of memory (infinite recursion) running a stochastic collocation

4 visualizzazioni (ultimi 30 giorni)
I'm trying to run an stochastic collocation for uncertainty quantification using Gauss-Hermite quadrature. Trying to run it gives me the error:
"Out of memory. The likely cause is an infinite recursion within the program.
Error in Stochastic_Collocation (line 29)
funcEval=frandomness(pt1,pt2,pt3,pt4);"
Any help would be appreciated.
function [mean_f, SDEV] = Stochastic_Collocation(x,nomforce)
%Calculates the mean and the standard deviation envelopes
% define function and Gaussian variables
fnom0 = nomforce(1);
L = 7.5;
%Random variable noises and standard deviations
mu1 = 0;
sigma1 = fnom0/(10);
mu2 = 0;
sigma2 = fnom0/(20);
mu3 = 0;
sigma3 = fnom0/(30);
mu4 = 0;
sigma4 = fnom0/(40);
% 5 point Gauss - Hermite quadrature. Taken from http://www.efunda.com/math/num_integration/findgausshermite.cfm
xi = [ -2.020182870456085632929; -0.958572464613818507112; 0.0;0.958572464613818507112;2.020182870456085632929];
wts = [0.01995324205904591320774;0.3936193231522411598285 ;0.9453087204829418812257;0.393619;0.0199532]./ sqrt (pi) ; % adjusted weights !
%Quadrature Loops
mean_f = 0;
fsquared = 0;
for i1 = 1:size( xi ,1)
pt1 = sqrt (2) * sigma1 * xi ( i1 ) + mu1 ;
for i2 = 1: size ( xi ,1)
pt2 = sqrt (2) * sigma2 * xi ( i2 ) + mu2 ;
for i3 = 1:size( xi ,1)
pt3 = sqrt (2) * sigma3 * xi ( i3 ) + mu3 ;
for i4 = 1: size ( xi ,1)
pt4 = sqrt (2) * sigma4 * xi(i4) + mu4 ;
funcEval=frandomness(pt1,pt2,pt3,pt4);
weights = wts(i1)*wts(i2)*wts(i3)*wts(i4);
mean_f = mean_f + weights*funcEval;
fsquared = fsquared + weights*(funcEval.^2);
end
end
end
end
SDEV = real(sqrt(fsquared - mean_f.^2)); %#ok<NASGU>
%test routine
for i = 1:100
W = 0.5*500*9.8; % half of the operational weight, N
Nelem=30;
nomforce = (2*(2.5*W)/(L^2))*[L:-L/Nelem:0].'; %#ok<NBRAK> % loading at manueuver
x = [0:L/Nelem:L].'; %#ok<NBRAK>
randomness = 0;
for n = 1:4
randomness = randomness + (randn*nomforce(1)/(10*n))*cos(x.*((2*n-1)*pi/(2*L)));
end
force = nomforce + randomness;
[mean, stdev] = Stochastic_Collocation(x,nomforce);
figure(1)
plot(x,force,'b',x,nomforce,'-r')%,x,standev,'--r')
%forceall(L,i)=force;
hold on
plot(x,mean,'-r',x,mean - 6*stdev,'--r',x,mean + 6*stdev,'--r')
end
%forceall(:,i) = force;
SDEV = transpose(std(transpose(forceall)));
plot(x,mean,'-r',x,mean - 6*SDEV,'-.b',x,mean + 6*SDEV,'-.b')
function randomness = frandomness(xi1, xi2, xi3, xi4) %The function for the stochastic Collocation
W = 0.5*500*9.8; % half of the operational weight, N
Nelem = 30;
xi = [xi1;xi2;xi3;xi4];
L = 7.5;
x = [0:L/Nelem:L].'; %#ok<NBRAK>
nomforce = (2*(2.5*W)/(L^2))*[L:-L/Nelem:0].'; %#ok<NBRAK>
randomness = 0;
for n = 1:4
randomness = randomness + xi(n)*cos((2*n-1)*x.*(pi/(2*L)));
end
randomness = nomforce + randomness;

Risposta accettata

Steven Lord
Steven Lord il 9 Dic 2019
[mean, stdev] = Stochastic_Collocation(x,nomforce);
You call your function inside itself, which causes your function to call itself, which causes your function to call itself, which ...
You need some code path through your function that doesn't cause it to call itself, and all your calls eventually need to go to that code path. Otherwise you're stuck in an infinite loop. See for example the factorial example in the "In computer science" section on this Wikipedia page. factorial gets called by itself over and over but with a smaller value of n each time. When it is called with n = 0, it doesn't call itself.

Più risposte (0)

Categorie

Scopri di più su Numerical Integration and Differential Equations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by