I'm new at MATLAB. I want to write code of this equations.
I tried with below code which is written by me.
k=-6:6;
n=-6:6
x= double(k>=0);
h=10*exp(-10*(n-k)).*double((n<=0));
fonk=double(k>=0).*(10*exp(-10*(n-k)).*double(k-n>=0));
for n=1:5
y=integral(#(k)fonk(k,n),6,6);
stem((-6:-6),y);
But It gives
Subscript indices must either be real positive integers or logicals.
Error in untitled2>#(k)fonk(k,n) (line 10)
y=integral(#(k)fonk(k,n),1,6);
Error in integralCalc/iterateScalarValued (line 315)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in untitled2 (line 10)
y=integral(#(k)fonk(k,n),1,6);
errors. I have been googling for this error but I cant find solution. Thanks for Helping.
Modifying your 5th line should be sufficient. You don't need 1~4th lines.
fonk=#(kv,nv) double(kv>=0).*(10*exp(-10*(nv-kv)).*double(kv-nv>=0));
for n=1:5
y(n)=integral(#(z) fonk(z,n),-6,6);
end
stem(y)
You need to define fonk as a function that takes two arguments otherwise integral does not understand it. Also you need to use a vector form of y to store results.
Related
I want to do the following integration:
In order to do integration using the function integral in matlab, I define the function
first and then apply integral. Below is my function:
OrderEpsilon2IntegrandIntegralIntegrand=#(T,s)lambda.*(T-s).*exp(-1.*lambda.*s);
OrderEpsilon2IntegrandIntegral=...
#(T,u)integral(#(t)OrderEpsilon2IntegrandIntegralIntegrand(T,t),u,T);
OrderEpsilon2Integrand=#(T,u)...
(T-u).*sigma(u).*v_0-...
OrderEpsilon2IntegrandIntegral(T,u).*exp(lambda.*u).*sigma(u).*v_0;
OrderEpsilon2Integral=...
#(t)arrayfun(#(T)integral(OrderEpsilon2Integrand(T,u),InitialTime,T),t);
However, I have the following error message:
Error using integral (line 85)
A and B must be floating-point scalars.
Error in (T,u)integral(#(t)OrderEpsilon2IntegrandIntegralIntegrand(T,t),u,T)
Error in #(T,u)(T-u).*sigma(u).*v_0-OrderEpsilon2IntegrandIntegral(T,u).*exp(lambda.*u).*sigma(u).*v_0
Error in #(u)OrderEpsilon2Integrand(2.3,u)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
What's the problem to make such error occurs? I have no ideas on the message. Also, I would like to know how to correct the codes. However, there are some constraints on correcting the codes:
First, do not write
explicitly.
Second, do not use integral2. If possible, I would like to define the integrand having the integral.
Why not?:
syms s u T t sg lm
%%sg=u^2+u+1; %%Define sigma expression in here
f=int((T-u)*sg-int(lm*(T-s)*exp(-lm*s),s,u,T)*exp(lm*u),u,t,T)
Resulting in:
T*t + T/lm + (T^2*sg)/2 - t/lm + (sg*t^2)/2 - T^2/2 - 1/lm^2 - t^2/2 + (exp(-T*lm)*exp(lm*t))/lm^2 - T*sg*t
I have a 10^5 square sparse matrix called pbAttack. Each element represents if there is connection between node i and node j. If there is connection, pbAttack(i,j) = 1. Otherwise, pbAttack (i,j) = 0. Then I want to use it following this tutorial: Matlab Autoencoders. I use the same code as in the linked Matlab tutorial. I only change it to use my data.
However, I got following errors:
Error using bsxfun
Out of memory. Type HELP MEMORY for your options.
Error in mapminmax.apply (line 8)
y = bsxfun(#plus,y,settings.ymin);
Error in mapminmax.create (line 44)
y = mapminmax.apply(x,settings);
Error in mapminmax (line 51)
[y,settings] = mapminmax.create(x,param);
Error in nnet.internal.configure.input (line 31)
[x,config] = feval(processFcns{j},x,processParams{j});
Error in network/configure (line 234)
net = nnet.internal.configure.input(net,i,X{i});
Error in nntraining.config (line 116)
net = configure(network(net),X,T);
Error in nntraining.setup>setupPerWorker (line 68)
[net,X,Xi,Ai,T,EW,Q,TS,err] = nntraining.config(net,X,Xi,Ai,T,EW,configNetEnable);
Error in nntraining.setup (line 43)
[net,data,tr,err] = setupPerWorker(net,trainFcn,X,Xi,Ai,T,EW,enableConfigure);
Error in network/train (line 335)
[net,data,tr,err] = nntraining.setup(net,net.trainFcn,X,Xi,Ai,T,EW,enableConfigure,isComposite);
Error in Autoencoder.train (line 490)
net = train(net,X,X,'useGPU',iYesOrNo(useGPU));
Error in trainAutoencoder (line 105)
autoenc = Autoencoder.train(X, autonet, paramsStruct.UseGPU);
Error in workflow_autoencoder (line 8)
autoenc1 = trainAutoencoder(pbAttack,hiddenSize,...
Are all these errors caused by the huge size of the matrix? Is there a work around so that it does not need so much memory? Thank you so much.
It just means that you don't have enough memory for this (since your matrix is not so sparse). You can try to repeat your code by reducing the size of the matrix.
I have the following expression:
phi=(a)int(exp(-x(1+(8*pibc)^2))dx),
where the integral is from 0 to inf, and a,b,c are constants.
I know phi, but I do not know the constants a,b and c. That is why I want to do a non linear least square fitting, as I am not sure I will obtain a straight line, in case of obtaining it I would change it.
The thing is that I think I have problems with defining the integral and introducing it in the final expression where I do the non linear least square fitting. Loads of errors appear once I run it.
The code is the following:
%I introduce the data
qy=[0.022 0.023 0.025 0.027 0.028 0.03 0.033 0.037 0.043 0.049 0.058 0.065 0.075 0.094 0.141 0.155 0.192 0.219 0.369];
QY=qy(end);
%I introduce initial guesses for my fitting
initial_guesses=[max(max(qy))/2 QY/10 max(max(qy))/2];
i_g(1,1)=initial_guesses(1,1);
i_g(2,1)=initial_guesses(1,2);
i_g(3,1)=initial_guesses(1,3);
%Let's define my integral
fun=#(x,parameters) exp(-x.*(1+(8*pi()*parameters(2)*parameters(3)).^2));
%We model the fitting
HoffForst_decaydiff=#(parameters) (qy(1:end)-parameters(1)*integral(#(x,parameters)fun,0,Inf));
%options = optimoptions(#lsqnonlin,'Algorithm','levenberg-marquardt');
options = optimoptions(#lsqnonlin,'Algorithm','levenberg-marquardt','TolFun',1e-7,'TolX',1e-7);
%perform NL LS fit
[fit_parameters, resnorm, fit_residuals]=lsqnonlin(HoffForst_decaydiff, i_g, [], [], options);
The errors that appear once I run the last line (lsqnonlin line) are:
Error using integralCalc/finalInputChecks (line 511)
Input function must return 'double' or 'single' values. Found 'function_handle'.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 83)
[q,errbnd] = vadapt(#AToInfInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in #(parameters)(qy(1:end)-parameters(1)*integral(#(x,parameters)fun,0,Inf))
Error in lsqnonlin (line 194)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue.
Can you help me, please?
Cheers!
I am trying to optimize the function using fminsearch and function handle
but, I get the error A and B must be floating point scalars. In detalis,
Error using integral (line 86)
A and B must be floating point scalars.
Error in #(x,p)(integral(#(n)((p(1)-p(2))*exp(n)),-inf,x+3))
Error in #(x)((x-f2(x,p)).^2)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
Error in #(p)(integral(#(x)((x-f2(x,p)).^2),-3,3))
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
How can I solve this?
i think x-3 become a problem but i cannot deal with it.
x should be variable in f2 in order to do integral with respect to x in q3
Thank you in advance
sigma=0.1;
f2=#(x,p)(integral(#(n)((p(1)-p(2))*exp(n)),-inf,x+3));
q3=#(p)(integral(#(x)((x-f2(x,p)).^2),-3,3));
[p, fval] = fminsearch(q3,[0.1 0.4]);
The problem is that when you integrate over x, the function "integrate" gives f2 a vector to evaluate. Which is the same reason you use the dot notation in the function "q3".
The quick fix is to use arrayfun around the "f2" - but you should really consider to adapt to what user20160 suggested in the comments, namely to make full functions, then it will be easier to debug and as it takes in a vector, you can make a for loop, which runs over the inputted endpoints. Standard for-loops are faster than standard arrayfun.
I am using the Matlab function lsqnonlin for estimating a vector of 5 values.
Before calling lsqnonlin, I am making a rough estimation of the 3 parameters, and afterwards the lsqnonlin is being initialised using these three parameters (the other two are constant)
In some cases, the following error stops the execution:
Error using eig
Input to EIG must not contain NaN or Inf.
Error in trust (line 30)
[V,D] = eig(H);
Error in trdog (line 110)
[st,qpval,po,fcnt,lambda] = trust(rhs,MM,delta);
Error in snls (line 320)
[sx,snod,qp,posdef,pcgit,Z] = trdog(x,g,A,D,delta,dv,...
Error in lsqncommon (line 150) [xC,FVAL,LAMBDA,JACOB,EXITFLAG,OUTPUT,msgData]=...
Error in lsqnonlin (line 237)[xCurrent,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
Do you have any suggestion?