What does mean by ... when defining custom netowrk [duplicate] - matlab

What is the ellipsis for in this Matlab statement?
frame = insertObjectAnnotation(frame, 'rectangle', ...
bboxes, labels);
...I could not find in their online doc.

http://www.mathworks.com/help/matlab/matlab_prog/continue-long-statements-on-multiple-lines.html
Continue Long Statements on Multiple Lines
This example shows how to continue a statement to the next line using ellipsis (...).
s = 1 - 1/2 + 1/3 - 1/4 + 1/5 ...
- 1/6 + 1/7 - 1/8 + 1/9;

Related

Plot function in TikZ from an array of functions

I am trying to create a code that automatically draws splines (parametric curves of arbitrary degree—not only cubic—defined as piecewise functions of the variable \t) in 2D. I will compute those functions within an .Rnw file and I’d want my TikZ code to plot the different pieces within a for loop. My problem is that I don’t know how to create (and access) an array of such functions.
I provide a very simplified MWE here
\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\def\npcs{2}
% Functions defined separately
% 1st section
\def\fxa{-2* \t^0 + 2* \t^1 + 0* \t^2}
\def\fya{2* \t^0 + 2* \t^1 -1* \t^2}
% 2nd section
\def\fxb{-2* \t^0 + 2* \t^1 + 0* \t^2}
\def\fyb{1* \t^0 + 4* \t^1 -2* \t^2}
% ... as many times as sections in the piecewise functions
% Functions defined with an array
\def\fxall{{-2* \t^0 + 2* \t^1 + 0* \t^2}, {-2* \t^0 + 2* \t^1 + 0* \t^2}} % ... as many elements as sections in the piecewise functions
\def\fyall{{2* \t^0 + 2* \t^1 -1* \t^2}, {1* \t^0 + 4* \t^1 -2* \t^2}} % ... as many elements as sections in the piecewise functions
\draw[thick, domain = 0:1, smooth, variable = \t, color = red!60]
plot (\fxa, \fya);
\draw[thick, domain = 1:2, smooth, variable = \t, color = red!20]
plot (\fxb, \fyb);
% This is the part that is not working!!
\foreach \n [evaluate = \n as \tstart using int(\n - 1), evaluate = \n as \color using int(100 - 40*\n)] in {1,...,\npcs}
{
\draw[thick, domain = \tstart:\n, smooth, variable = \t, color = red!\color]
plot (\fxall[\tstart], \fyall[\tstart]);
}
\end{tikzpicture}
\end{document}
If I define the functions by hand: \fxa, \fxb, etc, \fya, \fyb, etc… then I would have no problem in plotting all of them, but that obviously does not automatize the problem.
However, when I try to automatize the plotting with a \foreach loop, I get an error. I have tried different ways of declaring the variables \fxall and \fyall (with double braces, with parentheses…) but nothing worked. I get a cascade of massage errors like
Missing $ inserted. }
Extra }, or forgotten $. }
Any ideas on how to fix this?
Thanks in advance!!

fsolve/fzero: No solution found, appears regular

I am trying to do the following algorithm using fsolve or fzero:
K5=8.37e-2
P=1
Choose an A
S2=(4*K5/A)^(2/3)
S6=3*S2
S8=4*S2
SO2 = (5*P)/149 - (101*S2)/149 - (293*S6)/149 - (389*S8)/149
H2O = (40*P)/149 + (556*S2)/447 + (636*S6)/149 + (2584*S8)/447
H2S = 2*SO2
newA = (H2O)^2/(SO2)^3
Repeat until newA=oldA
The main thing to solve is K5=1/4 * A * S2^3/2. It is from this that S2 is calculated in the first place.
So here is what I did in Matlab:
function MultipleNLEexample
clear, clc, format short g, format compact
Aguess = 300000; % initial guess
options = optimoptions('fsolve','Display','iter','TolFun',[1e-9],'TolX',[1e-9]); % Option to display output
xsolv=fsolve(#MNLEfun,Aguess,options);
[~,ans]=MNLEfun(xsolv)
%- - - - - - - - - - - - - - - - - - - - - -
function varargout = MNLEfun(A);
K5 = 8.37e-2;
S2 = (4*K5/A)^(2/3);
S6 = 3*S2;
S8 = 4*S2;
P=1; %atm
SO2 = (5*P)/149 - (101*S2)/149 - (293*S6)/149 - (389*S8)/149;
H2O = (40*P)/149 + (556*S2)/447 + (636*S6)/149 + (2584*S8)/447;
newA=H2O^2/SO2^3;
fx=1/4*newA*S2^(3/2)-K5;
varargout{1} = fx;
if nargout>1
H2S = 2*SO2;
varargout{2} = ((2*S2+6*S6+8*S8)/(2*S2+6*S6+8*S8+H2S+SO2)*100);
end
I cannot get my code to run, I get the following error:
No solution found.
fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
selected value of the function tolerance.
I have tried setting the tolerances as low as 1e-20 but that did not change anything.
The way your system is set up, it is actually convenient to plot it and observe its behavior. I vectorized your function and plottedf(x) = MLNEfun(x)-x, where the output of MLNE(x) is newA. Effectively, you are interested in a fixed point of your system.
What I observe is this:
There is a singularity, and a root crossing, at A ~ 3800. We can use fzero since it is a bracketed root solver, and give it very tight bounds on the solution fzero(#(x)MLNEfun(x)-x, [3824,3825]) which produces 3.8243e+03. This is a couple order of magnitudes from your starting guess. There is no solution to your system near ~3e5.
Update
In my haste, I failed to zoom in on the plot, which shows another (well-behaved) root at 1.3294e+04. It is up to you to decide which is the physically meaningful one(s). Everything I say below still applies. Just start your guess near the solution you're interested in.
In response to comment
Since you want to perform this for varying values of K, then your best bet is to stick with fzero so long as you are solving for one variable, instead of fsolve. The reason behind this is that fsolve uses variants of Newton's method, which are not bracketed and will struggle to find solutions at singular points like this. fzero on the other hand, uses Brent's method which is guaranteed to find a root (if it exists) within a bracket. It is also much more well behaved near singular points.
MATLAB's implementation of fzero also searches for a bracketing interval before carrying out Brent's method. So, if you provide a single starting guess sufficiently close to the root, it should find it for you. Sample output from fzero below:
fzero(#(x)MLNEfun(x)-x, 3000, optimset('display', 'iter'))
Search for an interval around 3000 containing a sign change:
Func-count a f(a) b f(b) Procedure
1 3000 -616789 3000 -616789 initial interval
3 2915.15 -433170 3084.85 -905801 search
5 2880 -377057 3120 -1.07362e+06 search
7 2830.29 -311972 3169.71 -1.38274e+06 search
9 2760 -241524 3240 -2.03722e+06 search
11 2660.59 -171701 3339.41 -3.80346e+06 search
13 2520 -109658 3480 -1.16164e+07 search
15 2321.18 -61340.4 3678.82 -1.7387e+08 search
17 2040 -29142.6 3960 2.52373e+08 search
Search for a zero in the interval [2040, 3960]:
Func-count x f(x) Procedure
17 2040 -29142.6 initial
18 2040.22 -29158.9 interpolation
19 3000.11 -617085 bisection
20 3480.06 -1.16224e+07 bisection
21 3960 2.52373e+08 bisection
22 3720.03 -4.83826e+08 interpolation
....
87 3824.32 -5.46204e+48 bisection
88 3824.32 1.03576e+50 bisection
89 3824.32 1.03576e+50 interpolation
Current point x may be near a singular point. The interval [2040, 3960]
reduced to the requested tolerance and the function changes sign in the interval,
but f(x) increased in magnitude as the interval reduced.
ans =
3.8243e+03

what does ellipsis mean in a Matlab function's argument list?

What is the ellipsis for in this Matlab statement?
frame = insertObjectAnnotation(frame, 'rectangle', ...
bboxes, labels);
...I could not find in their online doc.
http://www.mathworks.com/help/matlab/matlab_prog/continue-long-statements-on-multiple-lines.html
Continue Long Statements on Multiple Lines
This example shows how to continue a statement to the next line using ellipsis (...).
s = 1 - 1/2 + 1/3 - 1/4 + 1/5 ...
- 1/6 + 1/7 - 1/8 + 1/9;

Matlab edge expansion vectorization

I have the following matlab code in a project of mine. image_working at this point is a logical image, the result of edge detection. The below loop expands each white point to be essentially a cross with width width (this is so a later call to imfill() will find more closed regions. The four if statements check that each point is within the original bounds.
[edge_row, edge_col] = find(image_working);
for width = 1:width_edge_widen
for i = 1:length(edge_row)
if (edge_row(i) + width <= m)
image_working(edge_row(i) + width, edge_col(i)) = 1;
end
if (edge_row(i) - width >= 1)
image_working(edge_row(i) - width, edge_col(i)) = 1;
end
if (edge_col(i) + width <= n)
image_working(edge_row(i), edge_col(i) + width) = 1;
end
if (edge_col(i) - width >= 1)
image_working(edge_row(i), edge_col(i) - width) = 1;
end
end
end
I suspect there's a good way to vectorize it and avoid the top-level loop, but I'm at a loss as to how to go about it. Simply indexing (like image_working(edge_row, edge_col)) doesn't work, since this will give give a rectangular region rather than the individual points. Linear indexing (calling inds = find(image_working)) is undesirable because it's difficult to do both vertical and horizontal shifts, although there may well be a vectorizable transform on the indices that I haven't thought of. Any advice?
First, a "vectorized" solution will not neseserally be the fastest here, this depends in how sparse your binary image is. Second, here are a few solutions that will be faster than your code:
First create a random binary image
im0=rand(2000)>0.999; % this means sparsity (density) ~ 1e-3
Solution 1 - for loop (for a cross of width 1, but you can change it as needed):
im=im0;
sd=size(im);
width=1;
[x y]=find(im((1+width):sd(1)-(width+1), (1+width):sd(2)-(width+1)));
x=x+width; y=y+width;
for n=1:numel(y)
im(x(n)-width:x(n)+width,y(n))=1;
im(x(n),y(n)-width:y(n)+width)=1;
end
Solution 2 - vectorized and one line (for a cross of width 1):
im=conv2(single(im0),[0 1 0; 1 1 1; 0 1 0],'same')>0;
Solution 3 - vectorized logical indexing (for a cross of width 1)
im =( im0(2:end-1,2:end-1) | im0(1:end-2,2:end-1) |...
im0(2:end-1,1:end-2) | im0(3:end ,2:end-1) |...
im0(2:end-1,3:end));
im =[zeros(1,size(im,2)); im; zeros(1,size(im,2))];
im= [zeros(size(im0,1),1) im zeros(size(im,1),1)];
You'll see that for sparse images the for loop will be faster than the other methods.
Solution 1: Elapsed time is 0.028668 seconds.
Solution 2: Elapsed time is 0.041758 seconds.
Solution 3: Elapsed time is 0.120594 seconds.
For less sparse images say (~1%) you can use the vectorized solution (Solution 2), as the for loop will very quickly become less efficient but I would check performance on your data before deciding.
Bonus edit, for fun I vectorized the cross filter in solution 2 to have arbitrary width as follows:
f=#(width) circshift(vander([1 zeros(1,2*width)]),[width -width]);
so im=conv2(single(im0),f(1),'same')>0; is equivalent to what is written in Solution 2, but now you can use any f(width_size) you want.

MATLAB vectorized code return wrong result

I'm taking a MATLAB course and have written the following code. One is a FOR LOOP and the other is a vectorization. The FOR LOOP returns the correct result but the vectorization does not. Can anyone tell me what I have coded incorrectly?
Should be for the following equation.
1 - 1/3 + 1/5 - 1/7 + 1/9 - ... - 1/1003 (sum is 0.7849 - converges
slowly to pi/4)
USE FOR LOOP
clc
clear
tic
sign = -1;
y=0;
for x=1:2:1003
sign=-sign;
y=y+sign/x;
end
disp(['For Loop calculated ' num2str(y), ' and ran for ' num2str(toc)])
USE VECTORIZATION
clear
tic
n=1:2:1003;
x=sum(1./n -1./(n+2));
disp(['Vectorization calculated ' num2str(x), ' and ran for ' num2str(toc)])
You could either change the formula you use in your summation (as suggested by Luis), or you could keep your formula and just change the step size you take in your n vector to this:
n = 1:4:1003;
In the vectorized code, replace the x line by this:
x = sum(1./n .* (-1).^(0:numel(n)-1))
The term after 1./n takes care of the alternating sign.
As it stands now, your code sum(1./n -1./(n+2)) is giving you 1+1/3+1/5+...+1/1003 - (1/3+1/5+...+1/1005), that is (after cancellation of terms), 1-1/1005.