for cycle in copulapdf - matlab

I have to perform a copulapdf analysis and i need to create 8 different figures starting from varying nu (in this code called nuvar) from 1 to 8. I'm new in Matlab. I tried to write this for cycle but it doesn't works. Can someone help me please?
for nuvar= 1:1:8
for numvar1= 1:1:8
r=0.5;
nu=1;
u = linspace(0,1,20);
[u1,u2] = meshgrid(u,u);
rho1 = copulaparam('t',r,nu);
H(nuvar, numvar1) = copulapdf('t',[u1(:),u2(:)],rho1,nuvar);
surf(u1,u2,reshape(y,20,20))
end
end
I also tried to correct the script in this way:
r=0.5;
nu=1;
u = linspace(0,1,20);
rho1 = copulaparam('t',r,nu);
[u1,u2] = meshgrid(u,u);
for numvar1= 1:1:8
H(nuvar, numvar1) = copulapdf('t',[u1(:),u2(:)],rho1,nuvar);
surf(u1,u2,reshape(y,20,20))
end
I have the same error "Subscripted assignment dimension mismatch".

Related

Generate a heterokskedastic series in MATLAB?

I am trying to generate a heteroskedastic error term in MATLAB.
I have found a good link to help here, the problem I am having is replicating this in MATLAB.
Here is my attempt:
n = [(1:100) (1:100)]';
sigma2 = n.^(1.3);
t = size(n,1);
for i = 1:200
eps(i) = normrnd(0, sqrt(sigma2(1)));
end
eps = eps'
h = archtest(eps)
However, the test for hetero indicates I still do not have heteroskedastic data, can anyone see where I am going wrong.
Looks like you're keeping sigma2 fixed in the first value inside the loop.
Replace
eps(i) = normrnd(0, sqrt(sigma2(1)));
with
eps(i) = normrnd(0, sqrt(sigma2(i)));

Not enough input arguments when using For Loop over an Integral

When I run the following code in matlab, I get the "Not enough input arguments."
It is not clear to me what I am doing wrong. I have checked it over an over again, but obviously what I intend and what the computer says I intend do not match up.
tau = 50e-3;
b= 1;
dt = .2;
time=1:dt:100;
w = 1; W= w;
inte = zeros(1, length(time));
inte(1) = 0;
Ds = #(s) (w*s-s+b)*(1/tau);
for i=1:length(time)
inte(i) = integral(Ds,0,time(i));
end
My hope was that the loop would integrate with different values of time from the time array. Instead, it appears to be looping through and only integrating once.
Any and all help is appreciated.
Update to include full error message:
% Validate the first three inputs.
narginchk(3,inf);
if ~isa(fun,'function_handle')
error(message('MATLAB:integral:funArgNotHandle'));
end
if ~(isscalar(a) && isfloat(a) && isscalar(b) && isfloat(b))
error(message('MATLAB:integral:invalidEndpoint'));
end
opstruct = integralParseArgs(varargin{:});
Q = integralCalc(fun,a,b,opstruct);

Matlab - setting up a nested parfor loop

I got trouble setting up a parfor loop in matlab. I know its rather easy but I am a bit stuck here thats why I would appreciate all help.
I have tried the following
valuesforM = zeros(901,100);
valuesforOPratio = zeros(100,1);
counter=1;
x = xlsread ('gtc.xlsx', 'A2:A10000');
y = xlsread ('gtc.xlsx','C2:C10000');
z = xlsread ('gtc.xlsx','B2:B10000');
parfor M = 100:1000;
counter=counter+1
for OPratio = 1:100;
NPVtotal = cut_off_optimisation(M,OPratio,x,y,z);
valuesforOPratio(OPratio)=NPVtotal;
end
valuesforM(M-99,:) = valuesforOPratio;
end
And i get the following error:
Error using senitivity_script_10000_steps (line 10)
Error: The variable valuesforOPratio in a parfor cannot be classified.
How can i fix this ? Thanks a lot.
EDIT
following the commments advice I tried the following:
valuesforM = zeros(901,100);
x = xlsread ('gtc.xlsx', 'A2:A10000');
y = xlsread ('gtc.xlsx','C2:C10000');
z = xlsread ('gtc.xlsx','C2:C10000');
parfor M = 100:1000;
NPVtotal = cut_off_optimisation(M,1:100,x,y,z);
valuesforM(M-99,:) = NPVtotal;
end
which gives the following error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in parforscript (line 8)
parfor M = 100:1000;
Any idea how to solve either of the both problems?
As you said in the comments, since counter is not required I have removed it and also counter won't work like that because different iterations will be running in a non sequential way and trying to update the same variable. Which is not allowed.
Next, you have to do valuesforOPratio = zeros(1,100) inside parfor loop because if you put it outside the loop then each iteration will be trying to access the same variable. This is not allowed. So that is why you are getting that error. When you put it inside, each iteration will be accessing a locally created variable. That is also the reason why you don't find it in base workspace. Here is the corrected code.
valuesforM = zeros(901,100);
x = xlsread ('gtc.xlsx', 'A2:A10000');
y = xlsread ('gtc.xlsx','C2:C10000');
z = xlsread ('gtc.xlsx','B2:B10000');
parfor M = 100:1000;
valuesforOPratio = zeros(1,100);
for OPratio = 1:100;
NPVtotal = cut_off_optimisation(M,OPratio,x,y,z);
valuesforOPratio(OPratio)=NPVtotal;
end
valuesforM(M-99,:) = valuesforOPratio;
end

MATLAB GUI User Input Update

I have a MATLAB GUI which takes values from the user input , do some calculation based on those and plot them in the GUI.
Everything is all right and working BUT when I change the values in the GUI it does not plot as expected and gives and error of dimension mismatch. However, there is no dimension mismatch and if I go to the script and press run again (the big green arrow) and then click plot again, it plots the values as expected.
I have initialized all of my values that I use but I can not see how can fix this. I need something like update command for all of the script each time I press the 'plot' button.
Any help is appreciated, many thanks!..
CODE:
a = str2double(get(handles.edit14,'String'));
b = str2double(get(handles.edit15,'String'));
c = str2double(get(handles.edit16,'String'));
d = str2double(get(handles.edit18,'String'));
e = str2double(get(handles.edit19,'String'));
f = str2double(get(handles.edit20,'String'));
for Veff=[a:b:c] %user input speeds (a,b,c) (comes from user)
q = 0.5*1.225*(Veff*1000/3600)^2;
F1=q*S; M1=q*S*Cref;
FX1=(F1*coef(:,1)); FZ1=(F1*coef(:,3)); MM1=(M1*coef(:,5));
W=[d*g:e*g:f*g]; %define mass range (d,e,f comes from user)
for lw=1:1:length(W)
XGEquation2max1 = ((((-FB*(Xa-Xb))-(((FX1(12)))*(Za-Zr))-(((FZ1(7))))*(Xa-Xr))-((max(MM1))))./W(lw)) + Xa;
CGPercent(lw,column) = (XGEquation2max1 - Cstart)/Cref * 100;
end
column=column+1;
end
speed_matrix = [a:b:c];
mass_matrix = [d:e:f];
ns = size(speed_matrix);
ns= ns(2);
count5 = 1;
for ns=1:1:ns
hold all
axes(handles.axes4)
plot(CGPercent(:,ns),transpose(mass_matrix)/1000)
count5 = count5 + 1;
end
Main problem is with the variables 'd,e,f' because when I change 'a,b,c' only (i.e speed) there is no problem.

Parfor-loop not working, how to fix?

I am trying to parallize two of my for-loops and run it on a remote cluster.
I am using matlabpool open local 12 at the beginning with matlabpool close at the end. The problem I am running into is that my parfor-loop cannot use my matric properly and I am not sure how I would rewrite it so that it works.
H = hadamard(n);
H = [H;-H];
P = setdiff(P,H,'rows');
[r,c] = size(P);
A = zeros(n,r);
parfor i=1:r
for j=1:n
d = P(i,:) + H(j,:);
A(j,i) = sum(d(:) ~= 0);
end
end
and:
u2Had = cell(2,r);
parfor i =1:r
u2Had{1,i} = min(A(:,i));
MinHadIndex = find(A(:,i) == u2Had{1,i});
u2Had{2,i} = MinHadIndex;
end
Those are the two segments of the code I am trying to parallize. Any help is much appreciated and if I need to add anymore information please ask.
I don't know what your problem is in the first part as it works fine (perhaps if you defined P better)
regarding the second part, you can only send information to and from parloops in narrow cases.
Here change your code to the following:
u2HadT = cell(1,r);
parfor i =1:r
temp = min(A(:,i));
MinHadIndex = find(A(:,i) == temp);
u2HadT{i} = {temp;MinHadIndex};
end
u2Had = cell(2,r);
for i =1:r
u2Had{1,i} = u2HadT{i}(1);
u2Had{2,i} = u2HadT{i}(2);
end