fminsearch on a function internally using matrices - matlab

I'm having trouble minimizing a rather complicated function:
% Current densities - psi is a 4x1 fourvector
j0 = #(psi) psi' * psi;
% Solutions
chi1 = #(n_,r_,kt_,theta_) ...
1/sqrt(2) * ...
[ exp(1i*n_*theta_) .* besselj(n_,kt_*r_); ...
exp(1i*(n_+1)*theta_) .* besselj(n_+1,kt_*r_) ];
chi2 = #(n_,r_,kt_,theta_) ...
1/sqrt(2) * ...
[ exp(1i*n_*theta_) .* besselj(n_,kt_*r_); ...
-exp(1i*(n_+1)*theta_) .* besselj(n_+1,kt_*r_) ];
uplus = #(n_,E_,m_,r_,kz_,kt_,theta_) ...
sqrt((E_+m_)/(4*m_)) * ...
[ chi1(n_,r_,kt_,theta_); ...
(kz_-1i*kt_)/(E_+m_) * chi2(n_,r_,kt_,theta_) ];
Here: n,E,m,kz,theta are all constants for any purpose. I need to fit j0 to a step function (1 for r=1...10), and the model function being the four-vector psi, consisting of summing uplus over kt=zeros of besselj(n,kt/10*r), so that besselj(0,kt*10) is zero. The problem is that fminsearch doesn't like my complicated setup:
uplus_reduced = #(r_,kt_) uplus(0,E,m,r_,kz,kt_,0);
error_function = #(r_,coeffs_) error_j0_stepfunction(r_,coeffs_,...
uplus_reduced);
coeffs(1,:) = fminsearch( #(r) error_function(r',coeffs(:,1)), r);
where error_j0_stepfunction is this:
function error = error_j0_stepfunction(r,coeffs,basisspinor)
% Zeros of BesselJ
Nzeros = 100;
lambda = [ besselzero(0,Nzeros,1)';
besselzero(1,Nzeros,1)';
besselzero(2,Nzeros,1)' ];
% calculate psi= sum over zeros
psi = zeros(4,length(r));
for k=1:length(coeffs)
size_psi = size(psi(:,:) )
size_coeffs = size(coeffs(k))
size_basisspinor = size( basisspinor(r(:)', lambda(1,k)/10))
psi(:,:) = psi(:,:) + coeffs(k) * ...
basisspinor(r(:), lambda(1,k)/10);
end;
% calculate density (j0)
density = zeros(1,length(r));
for k=1:length(r)
density(k) = j0(psi(:,k));
end;
% calculate square error
error = sum((1-density(:)).^2);
end
I hope I have been clear enough, and at the same time concise so this can be answered. Thanks for any help!
EDIT: The error I get is this (with output making it nonsensical to me):
size_psi = 4 1000
size_coeffs = 1 1
size_basisspinor = 4 1000
Error using +
Matrix dimensions must agree.
Error in error_j0_stepfunction (line 15)
psi(:,:) = psi(:,:) + coeffs(k) * ...
Error in #(r_,coeffs_)error_j0_stepfunction(r_,coeffs_,uplus_reduced)
Error in #(r)error_function(r',coeffs(:,1))
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
Error in dirac_stepfunction (line 17)
coeffs(1,:) = fminsearch( #(r) error_function(r',coeffs(:,1)), r);
Error in dirac (line 7)
dirac_stepfunction;
It should make enough sense to anyone reading the error and code above. coeffs is a 20x3 matrix, r is a 1x1000 (or 1000x1)

Your call to basisspinor in the size-statement has r(:)' as first argument, while the second call to basisspinor has just r(:). I guess the first version was also intended for the second call.

Related

MATLAB error using fzero function to solve the loop

My following code generates the plot of V and D values in figure 1. In the graph, the parabolas and straigh lines intersect, and I need to find the roots from the plot from a loop. So I tried to use fzero function, but the error appeared:
Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL functions to reduce operands to logical scalar values.
Error in fzero (line 326)
elseif ~isfinite(fx) || ~isreal(fx)
Error in HW1 (line 35)
x=fzero(fun,1);
My code is:
clear all; close all
W = 10000; %[N]
S = 40; %[m^2]
AR = 7;
cd0 = 0.01;
k = 1 / pi / AR;
clalpha = 2*pi;
Tsl=800;
figure(1);hold on; xlabel('V');ylabel('D')
for h=0:1:8;
i=0;
for alpha = 1:0.25:12
i=i+1;
rho(i)=1.2*exp(-h/10.4);
cl(i) = clalpha * alpha * pi/180;
V(i) = sqrt(2*W/rho(i)/S/cl(i));
L(i) = 0.5 * rho(i) * V(i) * V(i) * S * cl(i);
cd(i) = cd0 + k * cl(i) * cl(i);
D(i) = 0.5 * rho(i) * V(i) * V(i) * S * cd(i);
clcd(i) = cl(i)/cd(i);
p(i) = D(i)*V(i);
ang(i) = alpha;
T(i)=Tsl*(rho(i)/1.2).^0.75;
end
figure(1); plot(V,D); hold on
plot(V,T);
end
fun = #(V) 0.5*V.*V.*rho.*S.*cd-T;
x=fzero(fun,1);
Probably, I should not use the fzero function, but the task is to find the roots of V from a plot (figure 1). There are parabolas and straight lines respectively.
From the documentation for fzero(fun,x)
fun: Function to solve, specified as a handle to a scalar-valued function or the name of such a function. fun accepts a scalar x and returns a scalar fun(x).
Your function does not return a scalar value for a scalar input, it always returns a vector which is not valid for a function which is being used with fzero.
1.- Your codes doesn't plot V and D: Your code plots D(V) and T(V)
2.- T is completely flat, despite taking part in the inner for loop calculations with T(i)=Tsl*(rho(i)/1.2).^0.75; as it had to be somehow modified.
But in fact it remains constant for all samples of V, constant temperature (°C ?), and for all laps of the outer for loop sweeping variable h within [0:1:8].
The produced T(V) functions are the flat lines.
3.- Then you try building a 3rd function f that you put as if f(V) only but in fact it's f(V,T) with the right hand side of the function with a numerical expression, without a symbolic expression, the symbolic expression that fzero expects to attempt zero solving.
In MATLAB Zero finding has to be done either symbolically or numerically.
A symbolic zero-finding function like fzero doesn't work with numerical expressions like the ones you have calculated throughout the 2 loops for h and for alpha .
Examples of function expressions solvable by fzero :
3.1.-
fun = #(x)sin(cosh(x));
x0 = 1;
options = optimset('PlotFcns',{#optimplotx,#optimplotfval});
x = fzero(fun,x0,options)
3.2.-
fun = #sin; % function
x0 = 3; % initial point
x = fzero(fun,x0)
3.3.- put the following 3 lines in a separate file, call this file f.m
function y = f(x)
y = x.^3 - 2*x - 5;
and solve
fun = #f; % function
x0 = 2; % initial point
z = fzero(fun,x0)
3.4.- fzeros can solve parametrically
myfun = #(x,c) cos(c*x); % parameterized function
c = 2; % parameter
fun = #(x) myfun(x,c); % function of x alone
x = fzero(fun,0.1)
4.- So since you have already done all the numerical calculations and no symbolic expression is supplied, it's reasonable to solve numerically, not symbolically.
To this purpose there's a really useful function called intersections.m written by Douglas Schwarz available here
clear all; close all;clc
W = 10000; %[N]
S = 40; %[m^2]
AR = 7;
cd0 = 0.01;
k = 1 / pi / AR;
clalpha = 2*pi;
Tsl=800;
figure(1);
ax1=gca
hold(ax1,'on');xlabel(ax1,'V');ylabel(ax1,'D');grid(ax1,'on');
title(ax1,'1st graph');
reczeros={}
for h=0:1:8;
i=0;
for alpha = 1:0.25:12
i=i+1;
rho(i)=1.2*exp(-h/10.4);
cl(i) = clalpha * alpha * pi/180;
V(i) = sqrt(2*W/rho(i)/S/cl(i));
L(i) = 0.5 * rho(i) * V(i) * V(i) * S * cl(i);
cd(i) = cd0 + k * cl(i) * cl(i);
D(i) = 0.5 * rho(i) * V(i) * V(i) * S * cd(i);
clcd(i) = cl(i)/cd(i);
p(i) = D(i)*V(i);
ang(i) = alpha;
T(i)=Tsl*(rho(i)/1.2).^0.75;
end
plot(ax1,V,D); hold(ax1,'on');
plot(ax1,V,T);
[x0,y0]=intersections(V,D,V,T,'robust');
reczeros=[reczeros [x0 y0]];
for k1=1:1:numel(x0)
plot(ax1,x0,y0,'r*');hold(ax1,'on')
end
end
Because at each pair D(V) T(V) there may be no roots, 1 root or more than 1 root, it makes sense to use a cell, reczeros, to store whatever roots obtained.
To read obtained roots in let's say laps 3 and 5:
reczeros{3}
=
55.8850 692.5504
reczeros{5}
=
23.3517 599.5325
55.8657 599.5325
5.- And now the 2nd graph, the function that is defined in a different way as done in the double for loop:
P = 0.5*V.*V.*rho.*S.*cd-T;
figure(2);
ax2=gca
hold(ax2,'on');xlabel(ax2,'V');ylabel(ax2,'P');grid(ax2,'on');
title(ax2,'2nd graph')
plot(ax2,V,P)
plot(ax2,V,T)
[x0,y0]=intersections(V,T,V,P,'robust');
for k1=1:1:numel(x0)
plot(ax2,x0,y0,'r*');hold(ax2,'on')
end
format short
V0=x0
P0=y0
V0 =
86.9993
P0 =
449.2990

WORHP - Matlab interface, ERROR IN LEQSOL: symbolic step

I try to transform Hessian in matlab handle into Worhp structure. The idea is using random input to find nonzero-element in Hessian, something like:
% Hessian
H = #(x)[x(1) x(2); x(2) 0];
% evaluate for some random points
nr = 3;
S = zeros(2,2);
for i=1:nr
S = S + full(H(rand(2,1)) ~=0);
end
% get sparsity
S = S ~=0;
% convert everything to vectors for WORHP
[ HMrow, HMcol ] = find(S);
posCombined = find(S);
hess2vec(rand(2,1),posCombined,H)
function hVec = hess2vec(x, mu, scale,posCombined,Hess)
H = Hess(x,mu, scale);
hVec = H(posCombined);
end
And the hess2Vec is assigned to wCallback.hm in matlab-interface.
It works well with the given example, i.e. the example from Maltab-interface, whose hessian is a 2x2 matrix
However it failed with an error by our own project:
Error using worhp
WorhpIPLeqInit: ERROR IN LEQSOL: symbolic step! flag =
-1
Error in worhp_interface (line 82)
[xFinal,lambdaFinal,muFinal,solverstatus] =
worhp(wData, wCallback);
Perhaps someone knows what that means? What might be the cause?

Use Matlab/Maple to find roots of a nonlinear equation

I am having difficulty in finding roots of a nonlinear equation. I have tried Matlab and Maple both, and both give me the same error which is
Error, (in RootFinding:-NextZero) can only handle isolated zeros
The equation goes like
-100 + 0.1335600000e-5*H + (1/20)*H*arcsinh(2003.40/H)
The variable is H in the equation.
How do I find the roots (or the approximate roots) of this equation?
Matlab Code:
The function file:
function hor_force = horizontal(XY, XZ, Lo, EAo, qc, VA)
syms H
equation = (-1*ZZ) + (H/qc)*(cosh((qc/H)*(XZ- XB))) - H/qc + ZB;
hor_force = `solve(equation);`
The main file:
EAo = 7.5*10^7;
Lo = 100.17;
VA = 2002;
XY = 0;
ZY = 0;
XB = 50;
ZB = -2;
XZ = 100;
ZZ = 0;
ql = 40;
Error which Matlab shows:
Error using sym/solve (line 22)
Error using maplemex
Error, (in RootFinding:-NextZero) can only handle isolated zeros
Error in horizontal (line 8)
hor_force = solve(equation);
Error in main (line 34)
h = horizontal(XY, XZ, Lo, EAo, ql, VA)
http://postimg.org/image/gm93z3b7z/
You don't need the symbolic toolbox for this:
First, create an anonymous function that can take vectors at input (use .* and ./:
equation = #(H) ((-1*ZZ) + (H./qc).*(cosh((qc./H).*(XZ- XB))) - H./qc + ZB);
Second, create a vector that you afterwards insert into the equation to find approximately when the sign of the function changes. In the end, use fzero with x0 as the second input parameter.
H = linspace(1,1e6,1e4);
x0 = H(find(diff(sign(equation(H))))); %// Approximation of when the line crosses zero
x = fzero(equation, x0) %// Use fzero to find the crossing point, using the initial guess x0
x =
2.5013e+04
equation(x)
ans =
0
To verify:
You might want to check out this question for more information about how to find roots of non-polynomials.
In Maple, using the expression from your question,
restart:
ee := -100 + 0.1335600000e-5*H + (1/20)*H*arcsinh(2003.40/H):
Student:-Calculus1:-Roots(ee, -1e6..1e6);
[ 5 ]
[-1.240222868 10 , -21763.54830, 18502.23816]
#plot(ee, H=-1e6..1e6, view=-1..1);

Calculate the derivative of the sum of a mathematical function-MATLAB

In Matlab I want to create the partial derivative of a cost function called J(theta_0, theta_1) (in order to do the calculations necessary to do gradient descent).
The function J(theta_0, theta_1) is defined as:
Lets say h_theta(x) = theta_1 + theta_2*x. Also: alpha is fixed, the starting values of theta_1 and theta_2 are given. Let's say in this example: alpha = 0.1 theta_1 = 0, theta_2 = 1. Also I have all the values for x and y in two different vectors.
VectorOfX =
5
5
6
VectorOfX =
6
6
10
Steps I took to try to solve this in Matlab: I have no clue how to solve this problem in matlab. So I started off with trying to define a function in Matlab and tried this:
theta_1 = 0
theta_2 = 1
syms x;
h_theta(x) = theta_1 + t2*x;
This worked, but is not what I really wanted. I wanted to get x^(i), which is in a vector. The next thing I tried was:
theta_1 = 0
theta_2 = 1
syms x;
h_theta(x) = theta_1 + t2*vectorOfX(1);
This gives the following error:
Error using sym/subsindex (line 672)
Invalid indexing or function definition. When defining a
function, ensure that the body of the function is a SYM
object. When indexing, the input must be numeric, logical or
':'.
Error in prog1>gradientDescent (line 46)
h_theta(x) = theta_1 + theta_2*vectorOfX(x);
I looked up this error and don't know how to solve it for this particular example. I have the feeling that I make matlab work against me instead of using it in my favor.
When I have to perform symbolic computations I prefer to use Mathematica. In that environment this is the code to get the partial derivatives you are looking for.
J[th1_, th2_, m_] := Sum[(th1 + th2*Subscript[x, i] - Subscript[y, i])^2, {i, 1, m}]/(2*m)
D[J[th1, th2, m], th1]
D[J[th1, th2, m], th2]
and gives
Coming back to MATLAB we can solve this problem with the following code
%// Constants.
alpha = 0.1;
theta_1 = 0;
theta_2 = 1;
X = [5 ; 5 ; 6];
Y = [6 ; 6 ; 10];
%// Number of points.
m = length(X);
%// Partial derivatives.
Dtheta1 = #(theta_1, theta_2) sum(2*(theta_1+theta_2*X-Y))/2/m;
Dtheta2 = #(theta_1, theta_2) sum(2*X.*(theta_1+theta_2*X-Y))/2/m;
%// Loop initialization.
toll = 1e-5;
maxIter = 100;
it = 0;
err = 1;
theta_1_Last = theta_1;
theta_2_Last = theta_2;
%// Iterations.
while err>toll && it<maxIter
theta_1 = theta_1 - alpha*Dtheta1(theta_1, theta_2);
theta_2 = theta_2 - alpha*Dtheta2(theta_1, theta_2);
it = it + 1;
err = norm([theta_1-theta_1_Last ; theta_2-theta_2_Last]);
theta_1_Last = theta_1;
theta_2_Last = theta_2;
end
Unfortunately for this case the iterations does not converge.
MATLAB is not very flexible for symbolic computations, however a way to get those partial derivatives is the following
m = 10;
syms th1 th2
x = sym('x', [m 1]);
y = sym('y', [m 1]);
J = #(th1, th2) sum((th1+th2.*x-y).^2)/2/m;
diff(J, th1)
diff(J, th2)

How can I fix an undefined function error in MATLAB?

I'm trying to write the code for quadrature phase-shift keying (QPSK) with zeroforcing when N=2, and I got an error.
Here is the code:
Modulation = 'QPSK'
Decode_Method = 'ZeroForcing'
switch Modulation
case {'QPSK'}
Symbols = [ 1+j 1-j -1+j -1-j ]';
end
Symbols = Symbols.';
nSymbols = length(Symbols);
SNR_Array = [0.3 0.7 1.2 2.5 5 6.2 10 15.4 22 45 75.7 100.0];
nSNR = length(SNR_Array);
Ntest = 20;
N = 2;
for iSNR = 1 : nSNR
SNR = SNR_Array(iSNR);
Nerror = 0;
for i = 1:Ntest
H = randn(N,N) + j*randn(N,N);
X = Symbols( ceil( nSymbols*rand(N,1) ) )';
Noise = (randn(N,1) + j*randn(N,1))/sqrt(2)/sqrt(SNR);
Y = H*X + Noise;
switch Decode_Method
case {'ZeroForcing'}
X_Decode = Zero_Forcing(Y,H,Symbols);
end
end
Nerror = Nerror + length( find( X ~= X_Decode) );
end
Symbol_Error_Rate(iSNR) = Nerror/Ntest/N;
figure(1)
loglog(SNR_Array, Symbol_Error_Rate,'b')
hold on
xlabel('SNR')
ylabel('Symbol Error Ratio')
title('Symbol Error Ratio for NxN MIMO System')
And the error is:
??? Undefined function or method 'Zero_Forcing' for input arguments of type 'double'.
Error in ==> Untitled2 at 33
X_Decode = Zero_Forcing(Y,H,Symbols);
How can I fix this error?
The error indicates that MATLAB cannot find the function Zero_Forcing. If you have a function of that name, you should make sure it's on the MATLAB path, that is, a directory MATLAB knows about. Otherwise, you should write the function. It seems rather important.
Also, you may want to not call your function 'Untitled2', but give it a more meaningful name.