not enough input arguments fminsearch - matlab

I'm trying to write a script in MATLAB that graphs a function in three dimensions using the mesh function and then finds the maximum of the surface. This is my code so far:
%% Aquifer, 3D maximum search
figure(2)
[X,Y] = meshgrid(-10:.5:10,-10:.5:10);
h = #(x,y)-(1./(1+(x-.25).^2+(y-.5).^2+x+x.*y));
mesh(h(X,Y)) %graph aquifer surface
aquamax = fminsearch(h,[-5;-5])
When I run the code I get this error:
Error using #(x,y)-(1./(1+(x-.25).^2+(y-.5).^2+x+x.*y))
Not enough input arguments.
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
I've read up on the fminsearch function but I'm not that familiar with it (still a bit of a noob at Matlab). Do I need to rework the code or is it just how I've input things into fminsearch?

Your h function requires 2 scalar inputs, but fminsearch only does one input, possibly a vector. Change h to h = #(x)-(1./(1+(x(1)-.25).^2+(x(2)-.5).^2+x(1)+x(1).*x(2))); and see if that works.

Related

Plotting modes of circular waveguide in Matlab

I'm having some trouble with a script that I need to plot the TE and TM modes of a circular waveguide.
Everything is based on these formulas:
Right now I'm focused on the TE modes and what I have to plot is the field vector e_mn'' inside a squared-mesh equal to the radius of the waveguide.
To do it I need to compute the bessel function of the first kind and extract x_mn that correspond to the n-th root in which the function is equal to zero. These points are supposed to be real but positive.
I followed this example
https://www.mathworks.com/examples/matlab/community/22719-roots-of-a-bessel-function
but I get an error:
Undefined function 'isfinite' for input arguments of type> 'function_handle'. Error in roots (line 26) if ~all(isfinite(c)) Error
in circular (line 20)
x_mn=roots(J0)
This is my code. Can you help me?
clc
clear all
close all
a=20; %radius
m=0;
n=1;
%%
if m==0
ki_m=1;
else
ki_m=sqrt(2);
end
r=0:0.1:a;
J0 = #(r) besselj(0,r);
%J0 = besselj(m,r);
%plot(J0)
x_mn=roots(J0)
%plot(J0(x_mn))
%x_mn=abs(x_mn);
k_mn=x_mn./a;
F_mn=(ki_m*k_mn)./(J0(x_mn)*sqrt(pi*(x_mn^2-m^2)));
for r=0:1:a
for phi=0:1*pi/180:2*pi
e_mnR=-F_mn.*(J0(k_mn*r)/(k_mn.*r))*sin(m*phi);
e_mnPHI=F_mn.*J0(k_mn*r)*cos(m*phi);
end
end
e_mnR=abs(e_mnR);
e_mnPHI=abs(e_mnPHI);
X=0:1:a;
[X,Y] = meshgrid(1:1:a);
quiver(X,Y,e_mnR,e_mnPHI);
hold on
This is not my area of expertise so take this with a grain of salt, but I did some googling and think I know what's going on:
The code you are working from in the provided link defines:
J0 = chebfun(#(x) besselj(0,x),[0 100]);
Note the link from your reference to a Chebfun example is dead, but I read here that:
Chebfun is an open-source package for computing with functions to about 15-digit accuracy. Most Chebfun commands are overloads of familiar MATLAB commands...
The only reason the example you cited works is because J0 is defined using Chebfun, which overloads the matlab command roots with its own version that works on a function handle. Maybe read more here and try using this tool if you want to replicate that code example. Hope this helps...
You can use fzero instead of roots, but you confused polar and cartesian coordinates in the plot section and dimensions of vectors don't agree

Polyval issues after using polyint

Im trying to calculate the area of randomly generated graph, which is created from randomly generated x and y values and drawn using polyfit.
clear
clc
for i=1:8
x(i)= round((12+5).*rand - 5,0)
y(i)= round((7+6).*rand -6,0)
end
p=polyfit(x,y,5);
x1=-5:0.1:12;
y1=polyval(p,x1);
plot(x,y,'o')
hold on
plot(x1,y1)
y2=(x1)*0-5
plot(x1,y2)
hold off
syms x
S1=int(x.*0-5,x,-2,7)
pp=polyint(p,x)
S2=polyval(pp,-2)-polyval(pp,7)
S=S1+S2
However, I am getting this weird error that doesnt make any sense to me.
Undefined function 'filter' for input arguments of type 'sym'.
Error in polyval (line 56)
y = filter(1,[1 -x],p);
Why doesnt it allow me to use polyval after using polyint ? Its still a polynomial..
In other words. How could I change the end of the code to calculate the definite integral of the newly formed polynomial, which is always different

Matlab , Error while plotting Heaviside. (Vectors must be the same length.)

I am doing a problem on Hamming code in Matlab. I have generated a bit string of length 1190, for transmission. I am asked to display the string as a curve of step function.
After doing some researched i found that the Heaviside function can be used for display the bit string as unit step curve.
When I use the command fplot(heaviside(l_f),[0 ,10000]), to plot the curve, where l_f is the bit string of length 1190, I get this error
Error using fcnchk (line 106)
FUN must be a function, a valid string expression, or an inline
function object.
Error in fplot (line 60)
fun = fcnchk(fun);
Error in Untitled (line 88)
fplot(heaviside(l_f),[0 ,10000])
When i display using Plot, i.e plot(heaviside(l_f),[0 ,10000]), I get the error
Error using plot Vectors must be the same length.
Error in Untitled (line 88) plot(heaviside(l_f),[0 ,10000])
Anyway to plot the bit string as a curve of step function ?
fplot(heaviside(l_f),[0 ,10000]) won't plot since fplot requires a function as first parameter. But in here it is a matrix. So use plot instead. Next, dimension of heaviside(l_f) will be 1x1190 and dimension of [0 ,10000] is 1x2. So wont work since dimensions are different so use.
x=heaviside(l_f)
y=0:(10000+1)/length(l_f):10000;
plot(x,y);

nlinfit Dimension Error

I am using nonlinearfit tool in matlab.
I keep getting the following error:
Error using nlinfit (line 210)
MODELFUN must be a function that returns a vector of fitted values the same size as Y (1-by-100). The model function you provided returned a result that was 1-by-2.
One common reason for a size mismatch is using matrix operators (, /, ^) in your function instead of the corresponding element-wise operators (., ./, .^).
I found this question very similar to mine, but still I get the same error. I have tried calculating myfun on the console while using a vector as an input, which gives me output of correct dimension. It will be ton of help if anybody can point out the mistake.
% Defining the function
myfun = #(t,b)exp(t.*b(1)+b(2));
[y_a] = arrayfun(myfun,x_a);
% Using nonlinear least square minimization
beta0 = [1 1];
nlinfit(x,y,myfun, beta0)
Thanks in advance...:)
Edit: Found this to be working.
g = fittype('exp(k*x + a)');
[fit1,gof,fitinfo] = fit(x',y',g,'StartPoint',[1 1]);
The function used in nlinfit takes the parameter vector as its first argument, then the independent data vector. You want,
myfun = #(b,t)exp(t.*b(1)+b(2));
Note that you could just use * rather than .* in this instance too.

fminsearch multiple parameters matlab

I'm trying to use fminsearch with multiple parameters but I can't seem to even get it working with two. I've also tried using optimization tool in matlab but then I get:
Optimization running.
Error running optimization.
Not enough input arguments.
What i do:
fval = fminsearch(#g,[1 1])
The function g looks like this:
function r=g(x,y)
r=x.^3+3*x*y.^2+12*x*y;
end
but i get this:
Error using g (line 2)
Not enough input arguments.
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
Your function g takes two inputs, x and y, however you supply fminsearch one input, the vector [1 1]. You need to rewrite it so that fminsearch only needs a single vector as input, but then that vector is split into two numbers to input into g.
fminsearch(#(v) g(v(1),v(2)),[1 1])
This makes an anonymous function that takes a vector as input (v) and then uses the first element (v(1)) as the first input to g, and the second element as the second input.