ezplot not working unless global variable is written - matlab

I'm trying to plot the following function
% k-nn density estimation
% localSearcher is a handle class responsible for finding the k closest points to x
function z = k_nearest_neighbor(x, localSearcher)
% Total point count
n = localSearcher.getPointCount();
% Get the indexes of the k closest points to x
% (the k parameter is contained inside the localSearcher class)
idx = localSearcher.search(x);
% k is constant
k = length(idx);
% Compute the volume (i.e. the hypersphere centered in x that encloses every
% sample in idx)
V = localSearcher.computeVolume(x, idx);
% The estimate for the density function is p(x) = k / (n * V)
z = k / (n * V);
end
I know for sure that the above algorithm is correct, because I get a reasonable plot using the following function
% Plot the values of k_nearest_neighbor(x, searcher) by sampling it manually
function manualPlot(samples, searcher)
a = -2;
b = 2;
n = 1000;
h = (b - a) / n;
sp = linspace(a, b, n);
pt = zeros(n);
areas = zeros(n);
estimated_pdf = #(x)k_nearest_neighbor(x, searcher);
area = 0;
for i = 1 : length(sp)
x = sp(i);
pt(i) = estimated_pdf(x);
area = area + h * pt(i);
areas(i) = area;
end
figure, hold on
title('k-nn density estimation');
plot(sp, pt, sp, areas, ':r');
legend({'$p_n(x)$', '$\int_{-2}^{x} p_n(x)\, \, dx$'}, 'Interpreter', 'latex', 'FontSize', 14);
plot(samples,zeros(length(samples)),'ro','markerfacecolor', [1, 0, 0]);
axis auto
end
called by
function test2()
clear all
close all
% Pattern Classification (page 175)
samples = [71 / 800; 128 / 800; 223 / 800; 444 / 800; 475 / 800; 546 / 800; 641 / 800; 780 / 800];
% 3-nn density estimation
searcher = NaiveNearestSearcher(samples, 3);
manualPlot(samples, searcher);
end
which outputs
However, if I try to do the same thing with ezplot
% Plot the values of k_nearest_neighbor using ezplot
function autoPlot(samples, searcher)
estimated_pdf = #(x)k_nearest_neighbor(x, searcher);
figure, hold on
ezplot(estimated_pdf, [-2,2]);
title('k-nn density estimation');
legend({'$p_n(x)$', '$\int_{-2}^{x} p_n(x)\, \, dx$'}, 'Interpreter', 'latex', 'FontSize', 14);
plot(samples,zeros(length(samples)),'ro','markerfacecolor',[1,0,0]);
axis auto
end
I get the following incorrect result
No warnings are issued from the console.
It's like the searcher parameter passed to the anonymous function
estimated_pdf = #(x)k_nearest_neighbor(x, searcher);
ezplot(estimated_pdf, [-2,2]);
goes "out of scope" (or something) before ezplot terminates.
The really weird thing is that adding
function z = k_nearest_neighbor(x, localSearcher)
[... same identical code ... ]
global becauseWhyNotVector;
% becauseWhyNotVector(end + 1) = 1; NOT WORKING, I must use the x variable for some reason
becauseWhyNotVector(end + 1) = x;
end
apparently fixes the problem (!).
Here's the full source code, I'm using MATLAB R2011a.

Related

Find the minimum of a multi-variable function

Question: Find the minimum of f(x,y)=x^2+y^2-2*x-6*y+14 in the window [0,2]×[2,4] with increment 0.01 for x and y.
My approach: Find the first partial derivatives fx and fy. The critical points satisfy the equations fx(x,y) = 0 and fy(x,y) = 0 simultaneously. find the second order partial derivatives fxx(x,y), fyy(x,y) and fxy(x,y) in order to find D.
clc
clear all
syms x y
fun=x^2+y^2-2*x-6*y+14;
fx=diff(fun,x);
fy=diff(fun,y);
pt=solve(fx==0,fy==0);
sol = struct2array(pt)
fxx=diff(fx,x);
fyy=diff(fy,y);
fxy=diff(fx,y);
D=subs(fxx,[x y],[1 3])*subs(fyy,[x y],[1 3])-(subs(fxy,[x y],[1 3]))^2
fxx_val=subs(fxx,[x y],[1 3])
minimum_value=subs(fun,[x y],[1 3])
Am I doing the right thing about what the question asked? Besides what about the window and increment mentioned that question. Any hints or solution will be appreciated .
Thanks in advance .
Use function evaluation optimization method instead of gradient
Please read through the code
f = #(x,y)x.^2+y.^2-2.*x-6.*y+14;
% x range
x_lb = 0;
x_ub = 2;
% y range
y_lb = 2;
y_ub = 4;
step = 0.01;
% lower bound of x, initial guess as xmin
xmin = x_lb;
% lower bound of y, initial guess as ymin
ymin = y_lb;
% f at the lower bounds, initial fmin
fmin = f(xmin, ymin);
for x = x_lb:step:x_ub
for y = y_lb:step:y_ub
% function evaluation
fval = f(x, y);
%replace fmin if the newly evaluated f is less than the actual fmin
if fval < fmin
fmin = fval;
% save current x and y where f is minimum
xmin = x;
ymin = y;
end
end
end
Solution
xmin = 1;
ymin = 3;
fmin = 4;
I would suggest to make use of Matlab's capabilities to compute with matrices. Then, no loop is required.
% your function, look up anonymous functions
func = #(x,y) x.^2 + y.^2 - 2.*x - 6.*y + 14;
% get matrices for you x- and y-window
[xg, yg] = meshgrid(0:.01:2, 2:0.01:4);
% compute all in one call
result = func(xg,yg);
% find total minimum
minimum = min(result(:));
% find the index of the (first) minimum, for other equations, there might
% be more than one
ind = find(result==minimum, 1);
% Output the result
fprintf('The minimum (%d) is located at x: %d, y: %d.\n', minimum, xg(ind), yg(ind));

Plotting 2D equation with infinite sum

I'm trying to plot the following equation (let's call it "Equation 1"):
This is the code I'm testing:
clear all;
xl=0; xr=1; % x domain [xl,xr]
J = 10; % J: number of division for x
dx = (xr-xl) / J; % dx: mesh size
tf = 0.1; % final simulation time
Nt = 60; % Nt: number of time steps
dt = tf/Nt/4;
x = xl : dx : xr; % generate the grid point
u_ex = zeros(J+1,Nt);
for n = 1:Nt
t = n*dt; % current time
for j=1:J+1
xj = xl + (j-1)*dx;
suma = zeros(100 , 1);
for k= 1:100
suma(k) = 4/(((2*k-1)^2) *pi*pi);
suma(k) = suma(k) * exp(-((2*k-1)^2) *pi*pi*t) * cos(2*k-1)*pi*xj;
end
m = sum(suma);
u_ex(j, n)= 0.5 - m;
end
end
tt = dt : dt : Nt*dt;
figure(1)
surf(x,tt, u_ex'); % 3-D surface plot
xlabel('x')
ylabel('t')
zlabel('u')
The problem is that all I get is a flat surface:
Equation 1 is suppossed to be the solution of the following parabolic partial differential equation with boundary values:
And after getting the numerical solution, it should look like this:
This plot gets the right values at the boundaries x = 0 and x = 1. The plot of Equation 1 doesn't have those values at the boundaries.
My complete .m code (that plots both the numerical solution and Equation 1) is:
clear all; % clear all variables in memory
xl=0; xr=1; % x domain [xl,xr]
J = 10; % J: number of division for x
dx = (xr-xl) / J; % dx: mesh size
tf = 0.1; % final simulation time
Nt = 60; % Nt: number of time steps
dt = tf/Nt/4;
mu = dt/(dx)^2;
if mu > 0.5 % make sure dt satisy stability condition
error('mu should < 0.5!')
end
% Evaluate the initial conditions
x = xl : dx : xr; % generate the grid point
% store the solution at all grid points for all time steps
u = zeros(J+1,Nt);
u_ex = zeros(J+1,Nt);
% Find the approximate solution at each time step
for n = 1:Nt
t = n*dt; % current time
% boundary condition at left side
gl = 0;
% boundary condition at right side
gr = 0;
for j=2:J
if n==1 % first time step
u(j,n) = j;
else % interior nodes
u(j,n)=u(j,n-1) + mu*(u(j+1,n-1) - 2*u(j,n-1) + u(j-1,n-1));
end
end
u(1,n) = gl; % the left-end point
u(J+1,n) = gr; % the right-end point
% calculate the analytic solution
for j=1:J+1
xj = xl + (j-1)*dx;
suma = zeros(100 , 1);
for k= 1:100
suma(k) = 4/(((2*k-1)^2) *pi*pi);
suma(k) = suma(k) * exp(-((2*k-1)^2) *pi*pi*t) * cos(2*k-1)*pi*xj;
end
m = sum(suma);
u_ex(j, n)= 0.5 - m;
end
end
% Plot the results
tt = dt : dt : Nt*dt;
figure(1)
colormap(gray); % draw gray figure
surf(x,tt, u'); % 3-D surface plot
xlabel('x')
ylabel('t')
zlabel('u')
title('Numerical solution of 1-D parabolic equation')
figure(2)
surf(x,tt, u_ex'); % 3-D surface plot
xlabel('x')
ylabel('t')
zlabel('u')
title('Analytic solution of 1-D parabolic equation')
maxerr=max(max(abs(u-u_ex))),
The code is taken from the book "Computational Partial Differential Equations Using MATLAB" by Yi-Tung Chen, Jichun Li, chapter 2, exercise 3.
In short: I'm not asking about the differential equation or the boundary problem, I want to know is: Why am I getting a flat surface when plotting Equation 1? Am I missing a parenthesis?
I do not want to use the symsum function because it never stop the script execution and I want to learn how to plot Equation 1 with no using symsum.
I've tested this code with Matlab R2008b and Octave 4.2.1. I got the same results (even with sums of 1000, 10000 and 50000 terms in the for loop with the k variable).
Edit!
Thanks, Steve!
I was missing a couple of parenthesis near the cosine, the right code is:
clear all; % clear all variables in memory
xl=0; xr=1; % x domain [xl,xr]
J = 10; % J: number of division for x
dx = (xr-xl) / J; % dx: mesh size
tf = 0.1; % final simulation time
Nt = 60; % Nt: number of time steps
dt = tf/Nt/4;
mu = dt/(dx)^2;
if mu > 0.5 % make sure dt satisy stability condition
error('mu should < 0.5!')
end
% Evaluate the initial conditions
x = xl : dx : xr; % generate the grid point
% store the solution at all grid points for all time steps
u = zeros(J+1,Nt);
u_ex = zeros(J+1,Nt);
% Find the approximate solution at each time step
for n = 1:Nt
t = n*dt; % current time
% boundary condition at left side
gl = 0;
% boundary condition at right side
gr = 0;
for j=2:J
if n==1 % first time step
u(j,n) = j;
else % interior nodes
u(j,n)=u(j,n-1) + mu*(u(j+1,n-1) - 2*u(j,n-1) + u(j-1,n-1));
end
end
u(1,n) = gl; % the left-end point
u(J+1,n) = gr; % the right-end point
% calculate the analytic solution
for j=1:J+1
xj = xl + (j-1)*dx;
suma = zeros(1000 , 1);
for k= 1:1000
suma(k) = 4/(((2*k-1)^2) *pi*pi);
suma(k) *= exp(-((2*k-1)^2) *pi*pi*t) * cos((2*k-1)*pi*xj);
end
m = sum(suma);
u_ex(j, n)= 0.5 - m;
end
end
% Plot the results
tt = dt : dt : Nt*dt;
figure(1)
colormap(gray); % draw gray figure
surf(x,tt, u'); % 3-D surface plot
xlabel('x')
ylabel('t')
zlabel('u')
title('Numerical solution of 1-D parabolic equation')
figure(2)
surf(x,tt, u_ex'); % 3-D surface plot
xlabel('x')
ylabel('t')
zlabel('u')
title('Analytic solution of 1-D parabolic equation')
Now my Equation 1 looks much better:
Also Steve was right when pointing out that my numerical solution may be wrong. I didn't notice that the boundary values are for the derivatives of my function, not the actual values of the function. I'll ask my teacher about this.
Edit2!
Ok, I got it. To calculate the derivatives at the boundaries you have to use hint 2.21 in the same book:
% hint 2.21 given by the book
% it is better to calculate the boundary values after calculating the inner points inside the for j = 1:m loop because you will need them:
u(1, n) = u(2, n) - dx * gl; % the left-end point
u(J+1,n) = u(J, n) + dx * gr; % the right-end point
Now my numerical solution looks like my analytic solution :D
Matlab R2008b can't recognize the *= operator that Octave does. I'm not tested this operator in other versions of Matlab because I'm too poor.
Yvon: I think the analytical solution formula comes from the real part of a Fourier expansion, but authors don't tell how they got it.

Optim-nonlinear equation in matlab code

I updated the question to clarify it more. Here is a graph:
For the curve in the attached photo, I hope to draw the curve. I have its equation and it is after simplification will be like this one
% Eq-2
(b*Y* cos(v) + c - k*X*sin(v))^2 + ...
sqrt(k*X*(cos(v) + 1.0) + b*Y*sin(v))^2) - d = 0.0
Where:
v = atan((2.0*Y)/X) + c
and b, c, d and k are constants.
from the attached graph,
The curve is identified in two points:
p1 # (x=0)
p2 # (y=0)
I a new on coding so accept my apologize if my question is not clear.
Thanks
So, after your edit, it is a bit more clear what you want.
I insist that your equation needs work -- the original equation (before your edit) simplified to what I have below. The curve for that looks like your plot, except the X and Y intercepts are at different locations, and funky stuff happens near X = 0 because you have numerical problems with the tangent (you might want to reformulate the problem).
But, after checking your equation, the following code should be helpful:
function solve_for_F()
% graininess of alpha
N = 100;
% Find solutions for all alphae
X = zeros(1,N);
options = optimset('Display', 'off');
alpha = linspace(0, pi/2, N);
x0 = linspace(6, 0, N);
for ii = 1:numel(alpha)
X(ii) = fzero(#(x)F(x, alpha(ii)), x0(ii), options);
end
% Convert and make an X-Y plot
Y = X .* tan(alpha);
plot(X, Y,...
'linewidth', 2,...
'color', [1 0.65 0]);
end
function fval = F(X, alpha)
Y = X*tan(alpha);
% Please, SIMPLIFY in the future
A = 1247745517111813/562949953421312;
B = 4243112111277797/4503599627370496;
V = atan2(2*Y,X) + A;
eq2 = sqrt( (5/33*( Y*sin(V) + X/2*(cos(V) + 1) ))^2 + ...
(5/33*( Y*cos(V) - X/2* sin(V) ))^2 ) - B;
fval = eq2;
end
Results:
So, I was having fun with this (thanks for that)!
Different question, different answer.
The solution below first searches for the constants causing the X and Y intercepts you were looking for (p1 and p2). For those constants that best fit the problem, it makes a plot, taking into account numerical issues.
In fact, you don't need eq. 1, because that's true always for any curve -- it's just there to confuse you, and problematic to use.
So, here it is:
function C = solve_for_F()
% Points of interest
px = 6;
py = 4.2;
% Wrapper function; search for those constants
% causing the correct X,Y intercepts (at px, py)
G = #(C) abs(F( 0, px, C)) + ... % X intercept at px
abs(F(py, 0, C)); % Y intercept at py
% Initial estimate, based on your original equation
C0 = [5/33
1247745517111813/562949953421312
4243112111277797/4503599627370496
5/66];
% Minimize the error in G by optimizing those constants
C = fminsearch(G, C0);
% Plot the solutions
plot_XY(px, py, C);
end
function plot_XY(xmax,ymax, C)
% graininess of X
N = 100;
% Find solutions for all alphae
Y = zeros(1,N);
X = linspace(0, xmax, N);
y0 = linspace(ymax, 0, N);
options = optimset('Display', 'off',...,...
'TolX' , 1e-10);
% Solve the nonlinear equation for each X
for ii = 1:numel(X)
% Wrapper function for fzero()
fcn1 = #(y)F(y, X(ii), C);
% fzero() is probably the fastest and most intuitive
% solver for this problem
[Y(ii),~,flag] = fzero(fcn1, y0(ii), options);
% However, it uses an algorithm that easily diverges
% when the function slope is large. For those cases,
% solve with fminsearch()
if flag ~= 1
% In this case, the minimum of the absolute value
% is searched for (which should be zero)
fcn2 = #(y) abs(fcn1(y));
Y(ii) = fminsearch(fcn2, y0(ii), options);
end
end
% Now plot the X,Y solutions
plot(X, Y,...
'linewidth', 2,...
'color', [1 0.65 0]);
xlabel('X'), ylabel('Y')
axis([0 xmax+.1 0 ymax+.1])
end
function fval = F(Y, X, C)
% Unpack constants
b = C(1); d = C(3);
c = C(2); k = C(4);
% pre-work
V = atan2(2*Y, X) + c;
% Eq. 2
fval = sqrt( (b*Y*sin(V) + k*X*(cos(V) + 1))^2 + ...
(b*Y*cos(V) - k*X* sin(V) )^2 ) - d;
end

Weird thing with matlab's plotting

When I execute this plot function trying to plot my data as a solid red line it will not plot anything.
plot(1:n, exp(x), '-r')
However, if I change the specification from a solid red line to a green line of circles still using the exact same data as so,
plot(1:n, exp(x), 'og')
it will plot! Why??
Here is all the code if needed.
clear all;
close all;
fprintf('\n\nJustin White Th-9\n\n')
x = input('Input the value of x to be approximated: ');
se = input('Input the target approximate perecent relative error, se: ');
[apre, macexp, n] = f_macexpF15(x, se);
macexp = macexp(1:end-1);
plotyy( 1:n, macexp, 1:n, apre);
hold on;
plot(1:n, exp(x), '-r')
And function it calls here
function[apre, macexp, n] = f_macexpF15(x, se)
fprintf('\nJustin White Th-9\n')
apre = 100*ones(1,3);
ms = [36 22 10];
macexp(1) = 1;
j = 1;
n = 1;
%% comments
while apre >= se
macexp(j+1) = macexp(j) + x^j/factorial(j);
apre(j) = 100 * ((macexp(j+1)-macexp(j))/macexp(j+1));
j = j + 1;
n = n + 1;
end
n = n - 1;
end
Thanks in advance
Easy, 1:n is a vector of length n, whereas x is only a scalar value if not entered correctly. So first of all check whether or not both vectors (1:n and x) are of same size.
Why? If you have two entries for the plot command, and one entry is a vector and the other is a scalar, then MATLAB treats that as if you entred n different plot commands (n for the length of the vector).

LogLog Plot in MATLAB (Estimating PI, error vs N)

TASK: Using the Monte_Carlo method, compute the approximation of PI for N=100,200,500,1000,2000,5000,10000,100000 and plot the Error of Approximation against N on a Log­Log Plot. Where Error=abs(Actual value­ - Approximate Value). In addition, compute PI using two other infinite series methods and compute pi for N=10,20,50,100,200,500,1000,2000,5000,10000. Plot out on the same graph the Error of Approximation on a Log­Log plot for all 2 formulae and the Monte­Carlo method.
Estimating PI using M_C Method.
clear all
clc
close all
%n = linspace(0, 100000, 100)
n = [100, 200, 500, 1000, 2000, 5000, 10000, 100000]
c = 0;
t = 0;
for q=1:length(n)
x = rand([1 n(q)]);
y = rand([1 n(q)]);
for i= 1:n(q)
t = t+1;
if x(i)^2 + y(i)^2 <= 1
c = c+1;
figure(2)
k(i) = x(i);
r(i) = y(i);
hold on;
else
figure(2)
p(i) = x(i);
j(i) = y(i);
end
end
figure(1)
hold on
if n(q) == 1000
plot(k, r, 'b.');
plot(p, j, 'r.');
end
ratio = c/t;
PI= 4*ratio
error = abs(pi - PI)
figure(2)
loglog(n(q), error, '-b');
end
loglog(n, error, 's-')
grid on
%% Calculating PI using the James Gregory
%n = 10000;
n = [10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000]
d = 1;
c = 4;
for j = 1:n
d = d + 2;
c = c + ((-1)^(j))*(4)*(1/d);
end
PI_1 = c;
error = abs(n - PI_1);
loglog(n,error, '-s')
display(c);
%% Calculating PI using the Leibniz Formula for PI
%n = 10000;
n = [10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000]
d = 1;
c = 1;
for k = 1:n
d = d + 2;
c = c + ((-1)^k)*(1/d);
end
PI_2 = c*4;
error = abs(n - PI_2);
figure(3)
loglog(n, error, '-s')
The problem I'm having is that the loglog plots don't display the expected information.
Plotting Discussion
For the Monte Carlo plotting, the line
loglog(n, error, 's-')
following the for-loop overwrites all of the plotting done by
loglog(n(q), error, '-b');
because a hold('on') was never issued for figure(2).
Also, in both cases, the plots will look odd due both to the style options and the fact that error is not a vector:
The call loglog(n, error, 's-') will generate a series of disconnected boxes since n is a vector and error is a scalar; Matlab interprets the elements of n as separated data sets each associated with the same scalar value error (the error from the last iteration of the for-loop; plot([1,2],0,'s-') is another example).
The called loglog(n(q), error, '-b'); has a similar problem. Since the style calls for a "solid blue line" but the data passed to loglog is a scalar-scalar pair each iteration, nothing will appear. Matlab cannot form a line for scalar-scalar input (consider the line plot plot(1,1,'-b') versus the circle plot plot(1,1,'ob') as another example).
These problems can be fixed by changing error to a vector of length(n):
error = zeros(1,length(n)); % before for-loop
...
error(q) = abs(pi - PI); % inside the q-for-loop
and performing the loglog plot after the for-loop only (this is also a performance increase since plotting calls are heavy relative to computation).
Performance Discussion
Speaking of performance (to speed up your Monte Carlo), the crowning virtue of Monte Carlo integration, besides not succumbing to the curse of dimensionality, is its ridiculously parallelizable (i.e., vectorizable) nature.
And this is a great thing since vanilla Monte Carlo requires a lot of samples to get accurate results.
Also, Matlab's logical indexing allows for a nice semantic way to pull values satisfying a number of criteria.
With that said, your i-for-loop for the Monte Carlo method can be vetorized with the following code:
% % ----- i-for-loop replacement
% Determine location of points
inCircle = (x.^2 + y.^2) <= 1;
% k = xIn, r = yIn
xIn = x(inCircle);
yIn = y(inCircle);
% p = xOut, j = yOut
xOut = x(~inCircle); % or x(not(inCircle));
yOut = y(~inCircle); % or y(not(inCircle));
% % ----- end of i-for-loop replacement
% Calculate MC pi and error
ratio = nnz(inCircle)/n(q);
PI = 4*ratio;
error(q) = abs(pi - PI);