Manipulation of cell arrays - arrays

I have two cell arrays of size [1X5]
K= {} {O1,O2,O3,O4} {O1,O3} {O1,O2,O3,O4} {O1,O2,O3,O4}
W= {O3}{O2}{O2,O3}{O2,O4}{O4}
I want to get as a result a cell array named S of size [1X4] as follows :
I put the contents of K{i} in every S{j} where j are the indices of the contents of W{i} (for example the cell W{3} has as content O2 and O3 so j=2,3. I put the content of K{3} in the cells S{2} and S{3} ).
After that I add in every S{i} a content Oi and I eliminate redundancy in S.
The expected result is the following :
S={O1}{O1,O2,O3,O4}{O1,O3}{O1,O2,O3,O4}

You can use the union function for this as well as several loops:
function S = q47140074(K,W)
if nargin < 2
% Input data:
K = {"", ("O"+(1:4).').', ("O"+[1;3]).', ("O"+(1:4).').', ("O"+(1:4).').'};
W = {"O3", "O2", ("O"+(2:3).').', ("O"+[2;4]).', "O4"};
end
% Preprocess data:
Wn = cellfun(#stripO,W,'UniformOutput',false);
% Preallocation:
S = num2cell(strings(1, max(cellfun(#max,Wn))));
% Computation:
for ind1 = 1:numel(Wn)
tmpW = Wn{ind1};
for ind2 = 1:numel(tmpW)
S{tmpW(ind2)} = union(S{tmpW(ind2)}, K{tmpW(ind2)});
end
end
for ind1 = 1:numel(S)
S{ind1} = setdiff(union(S{ind1},"O" + ind1),"");
end
end
function out = stripO(in)
out = str2double(strip(in,'left','O'));
end
Result:

Here is a solution using accumarray and unique:
K= {{} ,{'O1','O2','O3','O4'} ,{'O1','O3'} ,{'O1','O2','O3','O4'} ,{'O1','O2','O3','O4'}};
W= {{'O3'},{'O2'},{'O2','O3'},{'O2','O4'},{'O4'}};
subs = sscanf(cell2mat([W{:}]),'O%d');
m = max(subs);
subs = [subs;(1:m).'];
vals = repelem(1:numel(W),cellfun(#numel,W));
vals = [vals numel(K)+1:numel(K)+m]
K = [K num2cell(cellstr(num2str((1:m).','O%d'))).'];
%If your data are string scalars use the following K
%K = [K num2cell(string(cellstr(num2str((1:m).','O%d')))).']
result = accumarray(subs,vals,[],#(x){unique([K{x}])})
result =
{
[1,1] =
{
[1,1] = O1
}
[2,1] =
{
[1,1] = O1
[1,2] = O2
[1,3] = O3
[1,4] = O4
}
[3,1] =
{
[1,1] = O1
[1,2] = O3
}
[4,1] =
{
[1,1] = O1
[1,2] = O2
[1,3] = O3
[1,4] = O4
}
}

Related

How do I provide dimensions of an array to be reshaped as a vector variable?

I want to reshape the array G in every iteration, and the shape for new dimensions are coming from vector (say) tensorSize. But the MATLAB reshape command does not accept the below given method,
tensorSize = [5,6,7,9,3,4];
r=[1,5,25,225,45,9,1];
G1(1) = {randn(1,tensorSize(1),r(2))};
G1(2) = {randn(r(2),tensorSize(2),r(3))};
G1(3) = {randn(r(3),tensorSize(3),r(4))};
G1(4) = {randn(r(4),tensorSize(4),r(5))};
G1(5) = {randn(r(5),tensorSize(5),r(6))};
G1(6) = {randn(r(6),tensorSize(6),1)};
for j = 1:length(tensorSize)-1
if j == 1
G = G1(j);
end
G = reshape(G,[],r(j+1));
H = reshape(G1(j+1),r(j+1),[]);
G = G*H;
G = reshape(G,tensorSize(1:j+1),[]);
end
I have also tried to use other alternatives like:
str2num(regexprep(num2str(tensorSize(1:j+1),),'\s+',','))
str2num(strjoin(cellstr(tensorSize(1:j+1)),','))
but they create a string and when converted to num, they are not comma separated. So the reshape option does not accept it.
Is there any work around?
Thanks to #beaker in the comment section below, for proposing this solution!
tensorSize = [5,6,7,9,3,4];
r=[1,5,25,225,45,9,1];
G1(1) = {randn(1,tensorSize(1),r(2))};
G1(2) = {randn(r(2),tensorSize(2),r(3))};
G1(3) = {randn(r(3),tensorSize(3),r(4))};
G1(4) = {randn(r(4),tensorSize(4),r(5))};
G1(5) = {randn(r(5),tensorSize(5),r(6))};
G1(6) = {randn(r(6),tensorSize(6),1)};
tensorSizeCell = {zeros(1,length(tensorSize))};
for i = 1:length(tensorSize)
tensorSizeCell(i) = {tensorSize(i)};
end
for j = 1:length(tensorSize)-1
if j == 1
G = cell2mat(G1(j));
end
G = reshape(G,[],r(j+1));
H = reshape(cell2mat(G1(j+1)),r(j+1),[]);
G = G*H;
G = reshape(G,tensorSizeCell{1:j+1},[]);
end

Why does this code has an error of incompatible array size?

*[I found that the error occurred at line 57, z2 = w12.a1 + b12; with incompatible array size,
May I know how to fix it? Basically the data is from the simulation on MATLAB Simulink, so the size of the array changes as we increase the inputs from the original code which only have 1 input type][1]
%% load training data (use training_data.mat attached file)
% initialize number of nodes, weights, biases, errors and gradients.
%epochs and mini-batch size
clc
data = [out.dutycycle out.voltage out.current]
sample=1000;
labels = data(1:sample,1);
y = labels'; %output vector
images = data(1:sample,2:3);
images(:,1) = images(:,1)/400;
images(:,2) = images(:,2)/100;
images = images'; %Input vectors
hn1 = 80; %Number of neurons in the first hidden layer
hn2 = 60; %Number of neurons in the second hidden layer
%Initializing weights and biases
w12 = randn(hn1,1000).*sqrt(2/1000);
w23 = randn(hn2,hn1)*sqrt(2/hn1);
w34 = randn(1,hn2)*sqrt(2/hn2);
b12 = randn(hn1,1);
b23 = randn(hn2,1);
b34 = randn(1,1);
%learning rate
eta = 0.0058;
%Initializing errors and gradients
error4 = zeros(1,1);
error3 = zeros(hn2,1);
error2 = zeros(hn1,1);
errortot4 = zeros(1,1);
errortot3 = zeros(hn2,1);
errortot2 = zeros(hn1,1);
grad4 = zeros(1,1);
grad3 = zeros(hn2,1);
grad2 = zeros(hn1,1);
epochs = 50;
m = 10; %Minibatch size
%% Training phase
for k = 1:epochs %Outer epoch loop
batches = 1;
for j = 1:sample/m
error4 = zeros(1,1);
error3 = zeros(hn2,1);
error2 = zeros(hn1,1);
errortot4 = zeros(1,1);
errortot3 = zeros(hn2,1);
errortot2 = zeros(hn1,1);
grad4 = zeros(1,1);
grad3 = zeros(hn2,1);
grad2 = zeros(hn1,1);
for i = batches:batches+m-1 %Loop over each minibatch
%Feed forward
a1 = images(:,i);
z2 = w12.*a1 + b12;
a2 = elu(z2);
z3 = w23*a2 + b23;
a3 = elu(z3);
z4 = w34*a3 + b34;
a4 = elu(z4); %Output vector
%backpropagation
error4 = (a4-y(:,i)).*elup(z4);
error3 = (w34'*error4).*elup(z3);
error2 = (w23'*error3).*elup(z2);
errortot4 = errortot4 + error4;
errortot3 = errortot3 + error3;
errortot2 = errortot2 + error2;
grad4 = grad4 + error4*a3';
grad3 = grad3 + error3*a2';
grad2 = grad2 + error2*a1';
end
%Gradient descent
w34 = w34 - eta/m*grad4;
w23 = w23 - eta/m*grad3;
w12 = w12 - eta/m*grad2;
b34 = b34 - eta/m*errortot4;
b23 = b23 - eta/m*errortot3;
b12 = b12 - eta/m*errortot2;
batches = batches + m;
end
fprintf('Epochs:');
disp(k) %Track number of epochs
[images,y] = shuffle(images,y); %Shuffles order of the images for next epoch
end
disp('Training done!')
%note : create folder for this experiment and save the parameters.
% don't forget to keep the folder in matlab path!
save('wfour.mat','w34');
save('wthree.mat','w23');
save('wtwo.mat','w12');
save('bfour.mat','b34');
save('bthree.mat','b23');
save('btwo.mat','b12');
%% Testing phase
% load testing data with labels ... (use testing_data.mat attached file)
testsample = 100;
labels = test(1:testsample,1);
y = labels';
images = test(1:testsample,2:3);
images(:,1) = images(:,1)/400;
images(:,2) = images(:,2)/100;%copy
images = images';
we34 = matfile('wfour.mat');
w4 = we34.w34;
we23 = matfile('wthree.mat');
w3 = we23.w23;
we12 = matfile('wtwo.mat');
w2 = we12.w12;
bi34 = matfile('bfour.mat');
b4 = bi34.b34;
bi23 = matfile('bthree.mat');
b3 = bi23.b23;
bi12 = matfile('btwo.mat');
b2 = bi12.b12;
success = 0;
n = testsample;
for i = 1:n
out2 = elu(w2*images(:,i)+b2);
out3 = elu(w3*out2+b3);
out = elu(w4*out3+b4);
big = 0;
num = 0;
for k = 1:10
if out(k) > big
num = k-1;
big = out(k);
end
end
if labels(i) == num
success = success + 1;
end
end
fprintf('Accuracy: ');
fprintf('%f',success/n*100);
disp(' %');
%% ELU activation function
function fr = elu(x)
f = zeros(length(x),1);
for i = 1:length(x)
if x(i)>=0
f(i) = x(i);
else
f(i) = 0.2*(exp(x(i))-1);
end
end
fr = f;
end
%% derivative of the ELU activation function
function fr = elup(x)
f = zeros(length(x),1);
for i = 1:length(x)
if x(i)>=0
f(i) = 1;
else
f(i) = 0.2*exp(x(i));
end
end
fr = f;
end
%% SHuffle function
function [B,v] = shuffle(A,y)
cols = size(A,2);
P = randperm(cols);
B = A(:,P);
v = y(:,P);
end
Your help is much appreciated, thank you
MATLAB Error :
Arrays have incompatible sizes for this operation.
Error in ANN_PV_Array (line 57)
z2 = w12.*a1 + b12;

Speed up Value function Iterations in Julia

I am coding a simple value function iteration in Julia. I posted the code below.
# ................................
# Solving the model
function Solver(P,uf,hf)
#unpack r, σrisk, ρ, η, β, θ, nx, m, μ, fhat, ub, lb, tol, maxite, ne = P;
# ..................................................
# Tauchen discretization
ly, Π = mytauch(μ, ρ, η, nx, m); # it is a tauchen discretization
# ..................................................
# states
y = exp.(ly);
b = [LinRange(lb,ub, ne)...];
yb = b .+ y';
p0 = findfirst(x-> x ==0, b);
uhy = uf.(hf(y, fhat));
# ..................................................
# Initial guess for value functions
vr0 = 1 / (1 - β) * uf.((r / (1 + r)) * b .+ y');
vd0 = (1 / (1 - β) * uhy)';
myup(vr,vd) = updateVF(vr, vd, b, yb, uhy, Π, p0, uf, P);
# ..................................................
# Convergence algorithm
dif = 1
ite = 1
vr1, vd1, bopt = (nothing,nothing, nothing);
while dif>tol && ite < maxite
vr1, bopt, vd1 = myup(vr0,vd0)
dif = max(maximum(abs.(vr1-vr0)), maximum(abs.(vd1-vd0)));
ite+=1;
vr0, vd0 = (vr1, vd1);
end
# ..................................................
# Policies functions
vf1, D, δ, q = postVR(vr1,vd1,Π,r)
#bb = repeat(b,1,nx);
vd1 = repeat(vd1,ne,1);
bprime = b[bopt];
PolFun = (vf=vf1 , vr=vr1, vd=vd1, bprime = bprime, q=q,D=D, δ=δ);
out2 = (b= b, y= y,ydef=hf(y, fhat),Π=Π, uhy = uhy, p0 = p0);
return PolFun, out2;
end
function updateVF(vr0, vd0,b,yb,uhy,Π, p0, uf, P)
#unpack r , β, θ, nx,ne = P;
# ..................................................
# Preliminaries
vf0, D0, δ0, q0 = postVR(vr0,vd0,Π,r)
evf0 = vf0*Π';
evd0 = vd0*Π';
qb = q0 .* b;
βevf0 = β*evf0;
# ..................................................
# Value and debt policy function update
# Value of Default
vd1 = uhy' + β*(θ*evf0[p0,:]'+(1-θ)*evd0);
# Value of Repayment
# Method 2: by column
vr1 = Array{Float32,2}(undef, ne, nx);
bopt = Array{CartesianIndex{2},2}(undef, ne, nx);
upvr1!(vr1,bopt,yb,qb,βevf0,nx)
return vr1, map(ii -> ii[1],bopt), vd1
end
function postVR(vr,vd,Π,r)
vf = max.(vr,vd);
D = 1*(vd .> vr);
δ = D*Π';
q = (1/(1+r)) * (1 .- δ);
return vf, D, δ, q;
end
function upvr1!(vr1,bopt,yb,qb,βevf0,nx)
#fastmath #inbounds Threads.#threads for i in 1:nx
vr1[:,i], bopt[:,i] = #views findmax(uf.(max.(0,yb[:,i] .- qb[:,i]')) .+ βevf0[:,i], dims=1);
end
end
The master code is:
P = ( r = 0.017, σrisk = 2.0, ρ = 0.945, η = 0.025, β = 0.953,
θ = 0.282,nx = 21, m = 3, μ = 0.0,fhat = 0.969,
ub = 0, lb = -0.4, tol = 1e-6, maxite = 2000, ne = 251);
uf(x) = -1/x;
hf(y::Array, fhat::Float64) = min.(y, fhat * mean(y));
#time polfun, o2 = Solver(P,uf,hf);
The running tipe after first compilation is ~1 second. How can I speed-up this code? I am asking it because in previous versions of Matlab, in my experience, Julia was 10x faster but with the last version (Matlab 2020, that I tried last week) its performance was close to Julia but as I increase the number of grid-points (ne > 1000) Matlab reported faster. So I suppose that I am not taking advantage of all functionalities of Julia.
Note: The loop for upvr1! overperforms that a broadcasting version
yb = reshape(yb,(ne,nx,1));
βevf01 = PermutedDimsArray(reshape(βevf0,(ne,nx,1)),[3,2,1]);
qb1 = PermutedDimsArray(reshape(qb,(ne,nx,1)),[3,2,1]);
vr1, bopt = #views findmax(uf.(yb .- qb1) .+ βevf01, dims=3)
return vr1[:,:,1], map(ii -> ii[3], bopt)[:,:,1], vd1

Matlab : How to highlight GLYCINE residues in my Ramachandran plot? [duplicate]

This question already has an answer here:
MATLAB : What is the mistake in my Ramachandran plot?
(1 answer)
Closed 8 years ago.
I am trying matlab to plot ramachandran plot, without using built in command. I have succeeded too. Now I wanted to spot the GLYCINEs alone in the scatter array. Any ideas how to do this? (link to 1UBQ.pdb file : http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=pdb&compression=NO&structureId=1UBQ)
% Program to plot Ramanchandran plot of Ubiquitin
close all; clear ; clc; % close all figure windows, clear variables, clear screen
pdb1 ='/home/devanandt/Documents/VMD/1UBQ.pdb';
p=pdbread(pdb1); % read pdb file corresponding to ubiquitin protein
atom={p.Model.Atom.AtomName};
n_i=find(strcmp(atom,'N')); % Find indices of atoms
ca_i=find(strcmp(atom,'CA'));
c_i=find(strcmp(atom,'C'));
X = [p.Model.Atom.X];
Y = [p.Model.Atom.Y];
Z = [p.Model.Atom.Z];
X_n = X(n_i(2:end)); % X Y Z coordinates of atoms
Y_n = Y(n_i(2:end));
Z_n = Z(n_i(2:end));
X_ca = X(ca_i(2:end));
Y_ca = Y(ca_i(2:end));
Z_ca = Z(ca_i(2:end));
X_c = X(c_i(2:end));
Y_c = Y(c_i(2:end));
Z_c = Z(c_i(2:end));
X_c_ = X(c_i(1:end-1)); % the n-1 th C (C of cabonyl)
Y_c_ = Y(c_i(1:end-1));
Z_c_ = Z(c_i(1:end-1));
V_c_ = [X_c_' Y_c_' Z_c_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_n - V_c_;
V_bc = V_ca - V_n;
V_cd = V_c - V_ca;
phi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
phi=cat(2,phi,-atan2d(y,x));
end
phi=phi(1,2:end);
X_n_ = X(n_i(2:end)); % (n+1) nitrogens
Y_n_ = Y(n_i(2:end));
Z_n_ = Z(n_i(2:end));
X_ca = X(ca_i(1:end-1));
Y_ca = Y(ca_i(1:end-1));
Z_ca = Z(ca_i(1:end-1));
X_n = X(n_i(1:end-1));
Y_n = Y(n_i(1:end-1));
Z_n = Z(n_i(1:end-1));
X_c = X(c_i(1:end-1));
Y_c = Y(c_i(1:end-1));
Z_c = Z(c_i(1:end-1));
V_n_ = [X_n_' Y_n_' Z_n_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_ca - V_n;
V_bc = V_c - V_ca;
V_cd = V_n_ - V_c;
psi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
psi=cat(2,psi,-atan2d(y,x));
end
psi=psi(1,2:end);
scatter(phi,psi)
box on
axis([-180 180 -180 180])
title('Ramachandran Plot for Ubiquitn Protein','FontSize',16)
xlabel('\Phi^o','FontSize',20)
ylabel('\Psi^o','FontSize',20)
grid
The output is :
EDIT : Is my plot correct? Biopython: How to avoid particular amino acid sequences from a protein so as to plot Ramachandran plot? has an answer which has slightly different plot.
The modified code is as below :
% Program to plot Ramanchandran plot of Ubiquitin with no glycines
close all; clear ; clc; % close all figure windows, clear variables, clear screen
pdb1 ='/home/devanandt/Documents/VMD/1UBQ.pdb';
p=pdbread(pdb1); % read pdb file corresponding to ubiquitin protein
atom={p.Model.Atom.AtomName};
n_i=find(strcmp(atom,'N')); % Find indices of atoms
ca_i=find(strcmp(atom,'CA'));
c_i=find(strcmp(atom,'C'));
X = [p.Model.Atom.X];
Y = [p.Model.Atom.Y];
Z = [p.Model.Atom.Z];
X_n = X(n_i(2:end)); % X Y Z coordinates of atoms
Y_n = Y(n_i(2:end));
Z_n = Z(n_i(2:end));
X_ca = X(ca_i(2:end));
Y_ca = Y(ca_i(2:end));
Z_ca = Z(ca_i(2:end));
X_c = X(c_i(2:end));
Y_c = Y(c_i(2:end));
Z_c = Z(c_i(2:end));
X_c_ = X(c_i(1:end-1)); % the n-1 th C (C of cabonyl)
Y_c_ = Y(c_i(1:end-1));
Z_c_ = Z(c_i(1:end-1));
V_c_ = [X_c_' Y_c_' Z_c_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_n - V_c_;
V_bc = V_ca - V_n;
V_cd = V_c - V_ca;
phi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
phi=cat(2,phi,-atan2d(y,x));
end
phi=phi(1,2:end);
X_n_ = X(n_i(2:end)); % (n+1) nitrogens
Y_n_ = Y(n_i(2:end));
Z_n_ = Z(n_i(2:end));
X_ca = X(ca_i(1:end-1));
Y_ca = Y(ca_i(1:end-1));
Z_ca = Z(ca_i(1:end-1));
X_n = X(n_i(1:end-1));
Y_n = Y(n_i(1:end-1));
Z_n = Z(n_i(1:end-1));
X_c = X(c_i(1:end-1));
Y_c = Y(c_i(1:end-1));
Z_c = Z(c_i(1:end-1));
V_n_ = [X_n_' Y_n_' Z_n_'];
V_n = [X_n' Y_n' Z_n'];
V_ca = [X_ca' Y_ca' Z_ca'];
V_c = [X_c' Y_c' Z_c'];
V_ab = V_ca - V_n;
V_bc = V_c - V_ca;
V_cd = V_n_ - V_c;
psi=0;
for k=1:numel(X_c)
n1=cross(V_ab(k,:),V_bc(k,:))/norm(cross(V_ab(k,:),V_bc(k,:)));
n2=cross(V_bc(k,:),V_cd(k,:))/norm(cross(V_bc(k,:),V_cd(k,:)));
x=dot(n1,n2);
m1=cross(n1,(V_bc(k,:)/norm(V_bc(k,:))));
y=dot(m1,n2);
psi=cat(2,psi,-atan2d(y,x));
end
psi=psi(1,2:end);
res=strsplit(p.Sequence.ResidueNames,' ');
angle =[phi;psi];
angle(:,find(strcmp(res,'GLY'))-1)=[];
scatter(angle(1,:),angle(2,:))
box on
axis([-180 180 -180 180])
title('Ramachandran Plot for Ubiquitn Protein','FontSize',16)
xlabel('\Phi^o','FontSize',20)
ylabel('\Psi^o','FontSize',20)
grid
which gives output (with no GLY) as below :
I would change this code block to use logical indexing
res=strsplit(p.Sequence.ResidueNames,' ');
angle =[phi;psi];
angle(:,find(strcmp(res,'GLY'))-1)=[];
Instead:
residues = strsplit(p.Sequency.ResidueNames,' ');
glycine = ismember(residues,'GLY');
angle = [phi;psi];
angleNoGLY= angle(:,~glycine);
Doing it this way, if you wanted to highlight glycine (or any other residue) you can easily call it out:
angleGLY = angle(:,glycine);
plot(angleNoGLY(1,:),angleNoGLY(2,:),'ob')
line(angleGLY(1,:),angleGLY(2,:),'Marker','o','Color','r','LineStyle','none')

Matlab- how can I change an array since it is seen as double

I need to iterate Newton-Raphson.The problem is:
For mmm=1:
1) If m=1 take c1=c1b and c2=1-c1 and do the loop for u1,2(i) and p1,2(i)
2)If m=2 take c1=c1+dc and c2=1-c1, and this time do the loop with new c1 and c2 for u1,2(i) and p1,2(i)
3) If m=3 take c1=(c1*st(1)-(c1-dc)*st(2))/(st(1)-st(2)) and do the loop for new c1 and c2.
Then increase the iteration number: mmm=2 ;
mmm keeps count of the number of N-R iterations. The first iteration has mmm=1, the second mmm=2, etc. (This particular run only do 2 iterations).
sumint are inside of the integrals. 'c1, c2 are the camber effects, u1 u2 are the velocities, p1 p2 are pressures.
Relevant part of the code:
ii=101;
ub = cell(2, 1);
ini_cond = [0,0];
for i = 1:2;
ub{i} = zeros(1,ii);
ub{i}(:, ii) = ini_cond(i) * rand(1, 1);
end
for i=1:ii;
x=i*dx;
fikness = fik*sin(pi.*x);
ub{1}(i) = (c1b-H1D*(x-0.5)+AD/2.*(x-0.5).^2)./(H1-0.5*fikness-A*(x-0.5));
ub{2}(i) = (c2b+H1D*(x-0.5)-AD/2.*(x-0.5).^2)./(1.-H1+0.5*fikness+A*(x-0.5));
end
mmm = 1;
c1 = c1b;
m = 1;
c2=1-c1;
u = cell(2, 1);
ini_cond = [0,0];
for i = 1:2;
u{i} = zeros(1,ii);
u{i}(:, ii) = ini_cond(i) * rand(1, 1);
end
for i=1:ii;
x=(i-1)*dx;
fikness = fik*sin(pi.*x);
u{1}(i) = (c1-H1D*(x-0.5)+AD/2.*(x-0.5).^2)./(H1-0.5*fikness-A*(x-0.5));
u{2}(i)= (c2+H1D*(x-0.5)-AD/2.*(x-0.5).^2)./(1.-H1+0.5*fikness+A*(x-0.5));
end
p = cell(2, 1);
q = cell(2, 1);
for i = 1:2;
p{i} = zeros(1,100);
q{i} = zeros(1,100);
end
p{1}(1) = 0.5*(1.-u{1}(1).^2);
q{1}(1) = 0;
p{2}(1) = 0.5*(1.-u{2}(1).^2);
q{2}(1) = 0;
for i = 2:ii;
q{1}(i) = q{1}(i-1)-dx*(u{1}(i-1)-ub{1}(i-1))./dt;
p{1}(i) = 0.5*(1.-u{1}(i).^2)+q{1}(i);
q{2}(i) = q{2}(i-1)-dx*(u{2}(i-1)-ub{2}(i-1))./dt;
p{2}(i) = 0.5*(1.-u{2}(i).^2)+q{2}(i);
end
st = zeros(2, 1);
st(1,:) = p{1}(100)-p{2}(100);
m = m+1;
if m==3;
c1=(c1*st(1)-(c1-dc)*st(2))/(st(1)-st(2));
c2=1-c1;
end
sumint = cell(2, 1);
for i = 1:2
sumint{i} = zeros(1,length(x));
end
sumint{1}(1) = 0.5*(p{2}(1)-p{1}(1));
sumint{2}(1) = 0.5*(p{2}(1)-p{1}(1)).*(-1/2);
for i = 2:100;
x=(i-1)*dx;
sumint{1}(i) = sumint{1}(i-1)+(p{2}(i)-p{1}(i));
sumint{2}(i) = sumint{2}(i-1)+(p{2}(i)-p{1}(i)).*(x-1/2);
end
The error is: ??? Attempted to access u.%cell(2); index out of bounds because numel(u.%cell)=1.
Error in ==> grab3_SmithWilson at 75 p{1}(i)=0.5*(1.-u{1}(i).^2)+q{1}(i);
You need to show us what you want to see. When I run the code you posted now, I find that first ub is printed as you have written, then each cell of ub is overwritten on each loop iteration. What I mean is that you are not putting values into the arrays that are stored in the cells, you are putting values in to the cells themselves. Are you sure that's what you want?
If you want to store the calculation in the elements of the arrays that are stored in the cells, the following will work:
for i=1:ii;
x=(i-1)*dx;
fikness=fik*sin(pi.*x);
ub{1}(i)=(c1b-H1D*(x-0.5)+AD/2*(x-0.5)^2)/(H1-0.5*fikness-A*(x-0.5));
ub{2}(i)=(c2b+H1D*(x-0.5)-AD/2*(x-0.5)^2)/(1-H1+0.5*fikness+A*(x-0.5));
end
>> ub
ub =
[1x101 double]
[1x101 double]
This is why I suggested reading about accessing parts of cells. Really though, this is just a guess until you tell us what you want from your script.

Resources