matlab: large system of coupled nonlinear equations - matlab

I'm trying to solve the a very large system of coupled nonlinear equations. Following this thread and the related help by Matalb (first example) I tried to wrote the following code:
%% FSOLVE TEST #2
clc; clear; close all
%%
global a0 a1 a2 a3 a4 h0 TM JA JB
a0 = 2.0377638272727268;
a1 = -7.105521894545453;
a2 = 9.234000147272726;
a3 = -5.302489919999999;
a4 = 1.1362478399999998;
h0 = 45.5;
TM = 0.00592256;
JA = 1.0253896074561006;
JB = 1.3079437258774012;
%%
global N
N = 5;
XA = 0;
XB = 15;
dX = (XB-XA)/(N-1);
XX = XA:dX:XB;
y0 = JA:(JB-JA)/(N-1):JB;
plot(XX,y0,'o')
[x,fval] = fsolve(#nlsys,y0);
where the function nlsys is as follows:
function S = nlsys(x)
global a1 a2 a3 a4 N TM h0 dX JA JB
H = h0^2/12;
e = cell(N,1);
for i = 2:N-1
D1 = (x(i+1) - x(i-1))./2./dX;
D2 = (x(i+1) + x(i-1) - 2.*x(i))./(dX^2);
f = a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3;
g = - H.* (a1 + 2*a2.*x(i) + 3*a3.*x(i).^2 + 4*a4.*x(i).^3)./(x(i).^5);
b = (H/2) .* (5*a1 + 8*a2.*x(i) + 9*a3.*x(i).^2 + 8*a4.*x(i).^3)./(x(i).^6);
e{i} = #(x) f + b.*(D1.^2) + g.*D2 - TM;
end
e{1} = #(x) x(1) - JA;
e{N} = #(x) x(N) - JB;
S = #(x) cellfun(#(E) E(x), e);
When I run the program, Matlab gives the following errors:
Error using fsolve (line 280)
FSOLVE requires all values returned by user functions to be of data type double.
Error in fsolve_test2 (line 32)
[x,fval] = fsolve(#nlsys,y0);
Where are my mistakes?
Thanks in advance.
Petrus

Related

1D finite element method in the Hermite basis (P3C1) - Problem of solution calculation

I am currently working on solving the problem $-\alpha u'' + \beta u = f$ with Neumann conditions on the edge, with the finite element method in MATLAB.
I managed to set up a code that works for P1 and P2 Lagragne finite elements (i.e: linear and quadratic) and the results are good!
I am trying to implement the finite element method using the Hermite basis. This basis is defined by the following basis functions and derivatives:
syms x
phi(x) = [2*x^3-3*x^2+1,-2*x^3+3*x^2,x^3-2*x^2+x,x^3-x^2]
% Derivative
dphi = [6*x.^2-6*x,-6*x.^2+6*x,3*x^2-4*x+1,3*x^2-2*x]
The problem with the following code is that the solution vector u is not good. I know that there must be a problem in the S and F element matrix calculation loop, but I can't see where even though I've been trying to make changes for a week.
Can you give me your opinion? Hopefully someone can see my error.
Thanks a lot,
% -alpha*u'' + beta*u = f
% u'(a) = bd1, u'(b) = bd2;
a = 0;
b = 1;
f = #(x) (1);
alpha = 1;
beta = 1;
% Neuamnn boundary conditions
bn1 = 1;
bn2 = 0;
syms ue(x)
DE = -alpha*diff(ue,x,2) + beta*ue == f;
du = diff(ue,x);
BC = [du(a)==bn1, du(b)==bn2];
ue = dsolve(DE, BC);
figure
fplot(ue,[a,b], 'r', 'LineWidth',2)
N = 2;
nnod = N*(2+2); % Number of nodes
neq = nnod*1; % Number of equations, one degree of freedom per node
xnod = linspace(a,b,nnod);
nodes = [(1:3:nnod-3)', (2:3:nnod-2)', (3:3:nnod-1)', (4:3:nnod)'];
phi = #(xi)[2*xi.^3-3*xi.^2+1,2*xi.^3+3*xi.^2,xi.^3-2*xi.^2+xi,xi.^3-xi.^2];
dphi = #(xi)[6*xi.^2-6*xi,-6*xi.^2+6*xi,3*xi^2-4*xi+1,3*xi^2-2*xi];
% Here, just calculate the integral using gauss quadrature..
order = 5;
[gp, gw] = gauss(order, 0, 1);
S = zeros(neq,neq);
M = S;
F = zeros(neq,1);
for iel = 1:N
%disp(iel)
inod = nodes(iel,:);
xc = xnod(inod);
h = xc(end)-xc(1);
Se = zeros(4,4);
Me = Se;
fe = zeros(4,1);
for ig = 1:length(gp)
xi = gp(ig);
iw = gw(ig);
Se = Se + dphi(xi)'*dphi(xi)*1/h*1*iw;
Me = Me + phi(xi)'*phi(xi)*h*1*iw;
x = phi(xi)*xc';
fe = fe + phi(xi)' * f(x) * h * 1 * iw;
end
% Assembly
S(inod,inod) = S(inod, inod) + Se;
M(inod,inod) = M(inod, inod) + Me;
F(inod) = F(inod) + fe;
end
S = alpha*S + beta*M;
g = zeros(neq,1);
g(1) = -alpha*bn1;
g(end) = alpha*bn2;
alldofs = 1:neq;
u = zeros(neq,1); %Pre-allocate
F = F + g;
u(alldofs) = S(alldofs,alldofs)\F(alldofs)
Warning: Matrix is singular to working precision.
u = 8×1
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
figure
fplot(ue,[a,b], 'r', 'LineWidth',2)
hold on
plot(xnod, u, 'bo')
for iel = 1:N
inod = nodes(iel,:);
xc = xnod(inod);
U = u(inod);
xi = linspace(0,1,100)';
Ue = phi(xi)*U;
Xe = phi(xi)*xc';
plot(Xe,Ue,'b -')
end
% Gauss function for calculate the integral
function [x, w, A] = gauss(n, a, b)
n = 1:(n - 1);
beta = 1 ./ sqrt(4 - 1 ./ (n .* n));
J = diag(beta, 1) + diag(beta, -1);
[V, D] = eig(J);
x = diag(D);
A = b - a;
w = V(1, :) .* V(1, :);
w = w';
x=x';
end
You can find the same post under MATLAB site for syntax highlighting.
Thanks
I tried to read courses, search in different documentation and modify my code without success.

Why do i get "Error using vertcat" in Matlab ode45 solver?

I have the following function to be solved by ODE45 solver of Matlab:
function f = odefun(t, y)
global mu ft
% y = [a, h, k, p, q, L, lla, lh, lk, lp, lq, lL];
a = y(1);
h = y(2);
k = y(3);
p = y(4);
q = y(5);
L = y(6);
lla = y(7);
lh = y(8);
lk = y(9);
lp = y(10);
lq = y(11);
lL = y(12);
n = sqrt(mu./a.^3); % mean motion
G = sqrt(1-h.^2-k.^2);
K = 1+p.^2+q.^2;
sL = sin(L);
cL = cos(L);
r = (a.*G.^2)./(1+h.*sL+k.*cL);
% components of B Matrix
B11 = 2.*(n.^-1).*(G.^-1).*(k.*sL-h.*cL);
B12 = 2.*(n.^-1).*a.*(r.^-1).*G;
B13 = 0;
B21 = -(n.^-1).*(a.^-1).*G.*cL;
B22 = (n.^-1).*(a^-2).*r.*(G.^-1).*(h+sL)+(n.^-1).*(a.^-1).*G.*sL;
B23 = -(n.^-1).*(a.^-2).*r.*(G.^-1).*k.*(p.*cL-q.*sL);
B31 = (n.^-1).*(a.^-1).*G.*sL;
B32 = (n.^-1).*(a.^-2).*r.*(G.^-1).*(k+cL)+(n.^-1).*(a.^-1).*G.*cL;
B33 = (n.^-1).*(a.^-2).*r.*(G.^-1).*h.*(p.*cL-q.*sL);
B41 = 0;
B42 = 0;
B43 = 0.5.*(n.^-1).*(a.^-2).*r.*(G.^-1).*K.*sL;
B51 = 0;
B52 = 0;
B53 = 0.5.*(n.^-1).*(a.^-2).*r.*(G.^-1).*K.*cL;
B61 = 0;
B62 = 0;
B63 = n.*(a.^2).*(r.^-2).*G+(n.^-1).*(a.^-2).*r.*(G.^-1).*(q.*sL-p.*cL);
B = [B11 B12 B13;
B21 B22 B23;
B31 B32 B33;
B41 B42 B43;
B51 B52 B53;
B61 B62 B63];
% costate vector
lambdaVec = [lla lh lk lp lq lL];
% control law vector
uvec = lambdaVec * B;
unorm = norm(uvec);
u = uvec./unorm;
u = u';
z_ = (B*u).*ft;
C = [0; 0; 0; 0; 0; n.*(a.^2)*(r.^-2).*G];
zdot = z_+C;
which should solve a system of 12 ODEs with 12 initial conditions, however when i run the code i get the following error:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in odefun (line 49)
B = [B11 B12 B13;
which refers to matrix B. what possibly goes wrong here?
B11, B12, ... are the components of the matrix which are defined based on function variables e.g. "y" vector.

artificial neural network in octave

I'm having trouble on an easy exercise about an artificial neural network with 2 features, a hidden layer of 5 neurons and two possible outputs (0 or 1).
My X matrix is a 51x2 matrix, and y is a 51x1 vector.
I know I'm not supposed to do the while E>1 but I wanted to see if eventually my error would be lower than 1
I'd like to know what I am doing wrong. My error doesn't seem to lower (around 1.5 no matter how much iterations I'm doing). Do you see in the code where I am doing a mistake? I'm supposed to use gradient descent.
function [E, v,w] = costFunction(X, y,alpha1,alpha2)
[m n] = size(X);
E = 1;
v = 2*rand(5,3)-1;
w = 2*rand(2,6)-1;
grad_v=zeros(size(v));
grad_w=zeros(size(w));
K = 2;
E = 2;
while E> 1
a1 = [ones(m,1) X];
z2 = a1 * v';
a2 = sigmoid(z2);
a2 = [ones(size(a2,1),1),a2];
z3 = a2 * w';
h = sigmoid(z3);
cost = sum((-y.*log(h)) - ((1-y).*log(1-h)),2);
E = (1/m)*sum(cost);
Delta1=0;
Delta2=0;
for t = 1:m
a1 = [1;X(t,:)'];
z2 = v * a1;
a2 = sigmoid(z2);
a2 = [1;a2];
z3 = w * a2;
a3 = sigmoid(z3);
d3 = a3 - y(t,:)';
d2 = (w(:,2:end)'*d3).*sigmoidGradient(z2);
Delta2 += (d3*a2');
Delta1 += (d2*a1');
end
grad_v = (1/m) * Delta1;
grad_w = (1/m) * Delta2;
v -= alpha1 * grad_v;
w -= alpha2 * grad_w;
end
end

"Solve" command with a vector in matlab

I have a problem with the equation shown below. I want to enter a vector in t2 and find the roots of the equation from different values in t2.
t2=[10:10:100]
syms x
p = x^3 + 3*x - t2;
R = solve(p,x)
R1 = vpa(R)
Easy! Don't use syms and use the general formula:
t2 = [10:10:100];
%p = x^3 + 3*x - t2;
a = 1;
b = 0;
c = 3;
d = -t2;
D0 = b*b - 3*a*c;
D1 = 2*b^3 - 9*a*b*c + 27*a^2*d;
C = ((D1+sqrt(D1.^2 - 4*D0.^3))/2).^(1/3);
C1 = C*1;
C2 = C*(-1-sqrt(3)*1i)/2;
C3 = C*(-1+sqrt(3)*1i)/2;
f = -1/(3*a);
x1 = f*(b + C1 + D0./C1);
x2 = f*(b + C2 + D0./C2);
x3 = f*(b + C3 + D0./C3);
Since b = 0, you can simplify this a bit:
% ... polynomial is the same
D0 = -3*a*c;
D1 = 27*a^2*d;
% ... the different C's are the same
f = -1/(3*a);
x1 = f*(C1 + D0./C1);
x2 = f*(C2 + D0./C2);
x3 = f*(C3 + D0./C3);
Trial>> syms x p
Trial>> EQUS = p == x^3 + 3*x - t2
It is unknown that you want to solve an equation or a system.
Suppose that you want to solve a system.
Trial>> solx = solve(Eqns,x)
But, I do not think you can find roots.
You can solve one equation.
Trial>> solx = solve(Eqns(3),x)
As far as I know, Maple can do this batter.
In general, loops should be avoided, but this is the only solution that hit my brain right now.
t2=[10:10:100];
pp=repmat([1,0,3,0],[10,1]);
pp(:,4)=-t2;
for i=1:10
R(i,:) =roots(pp(i,:))';
end

Plot of two variables varied simultaneously, on x-axis

My following code generates a graph with a looping variable for the x-axis. Specifically, eta_22 varies from 0 to 1, with loop iteration size of 0.01.
The code below the line is the source function file.
My question is: How can I generate a graph with eta_1 varying from 0 to 1, with loop iteration size of 0.01 as well? (I want a plot of AA on the y-axis, and eta_1, eta_2 varying from 0 to 1.)
My attempts: I have tried to create nested "for" loops, but the plot itself is looping. I have tried to put the plot line outside of the "for" loops as well, but that did not work.
Thanks for any help.
global Lambda mu mu_A mu_T beta tau eta_1 eta_2 lambda_T rho_1 rho_2 gamma
alpha = 100;
TIME = 365;
eta_22 = zeros(1,alpha);
AA = zeros(1,alpha);
for m = 1:1:alpha
eta_2 = m./alpha;
eta_22(m) = m./alpha;
Lambda = 531062;
mu = (1/70)/365;
mu_A = 0.25/365;
mu_T = 0.2/365;
beta = 0.187/365;
tau = 4/365;
lambda_T = 0.1;
rho_1 = 1/60;
rho_2 = (rho_1)./(270.*rho_1-1);
gamma = 1e-3;
eta_1 = 0;
S0 = 191564208;
T0 = 131533276;
H0 = 2405659;
C0 = 1805024;
C10 = 1000000;
C20 = 1000000;
CT10 = 500000;
CT20 = 500000;
y0 = [S0, T0, H0, C0, C10, C20, CT10, CT20];
[t,y] = ode45('SimplifiedEqns',[0:1:TIME],y0);
S = y(:,1);
T = y(:,2);
H = y(:,3);
C = y(:,4);
C1 = y(:,5);
C2 = y(:,6);
CT1 = y(:,7);
CT2 = y(:,8);
N = S + T + H + C + C1 + C2 + CT1 + CT2;
HIVinf1=[0:1:TIME];
HIVinf2=[beta.*(S+T).*(C1+C2)./N];
HIVinf=trapz(HIVinf1,HIVinf2);
AA(m) = HIVinf;
end
plot(100.*eta_22,AA./1000)
_____________________________________________________________________________________________________
function ydot = SimplifiedEqns(t,y)
global Lambda mu mu_A mu_T beta tau eta_1 eta_2 lambda_T rho_1 rho_2 gamma
S = y(1);
T = y(2);
H = y(3);
C = y(4);
C1 = y(5);
C2 = y(6);
CT1 = y(7);
CT2 = y(8);
N = S + T + H + C + C1 + C2 + CT1 + CT2;
ydot = zeros(8,1);
ydot(1)=Lambda-mu.*S-beta.*(H+C+C1+C2).*(S./N)-tau.*(T+C).*(S./N);
ydot(2)=tau.*(T+C).*(S./N)-beta.*(H+C+C1+C2).*(T./N)-(mu+mu_T).*T;
ydot(3)=beta.*(H+C+C1+C2).*(S./N)-tau.*(T+C).*(H./N)-(mu+mu_A).*H;
ydot(4)=beta.*(H+C+C1+C2).*(T./N)+tau.*(T+C).*(H./N)-(mu+mu_A+mu_T+lambda_T).*C;
ydot(5)=lambda_T.*C-(mu+mu_A+rho_1+eta_1).*C1;
ydot(6)=rho_1.*C1-(mu+mu_A+rho_2+eta_2).*C2;
ydot(7)=eta_1.*C1-(mu+rho_1+gamma).*CT1;
ydot(8)=eta_2.*C2-(mu+rho_2+gamma.*(rho_1)./(rho_1+rho_2)).*CT2+(rho_1).*CT1;
end
The simplest way:
eta_1=0:1/alpha:1;
eta_2=0:1/alpha:1;
lsize=length(eta_1) % I assume eta_1 and eta_2 are of the same size
for i=1:lsize
%Update your AA(i) here
end
plot(eta_1,AA,eta_2,AA)