I am trying to compute an integral using Matlab's dblquad. To do it I first wrote a script function. My code is
function z = IntRect(x, y)
%The variables
z0=20;
x0=15;
y0=20;
rl=sqrt(x.^2+y.^2+(z0/2)^2);
theta=acos(z0./(2*rl));
phi=atan(y./x);
%The function
z=(x0-z0*tan(theta).*cos(phi))*(y0-z0*tan(theta).*sin(phi))*(z0/2)^4;
z=z/rl.^3;
To compute the numerical integral I type in command window
z0=20;x0=15;y0=20;
Q = dblquad(#IntRect,0,x0/2,0,y0/2,1.e-6);
I get an error saying that
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> IntRect at 8
z=(x0-z0*tan(theta).*cos(phi))*(y0-z0*tan(theta).*sin(phi))*(z0/2)^4;
Error in ==> quad at 77
y = f(x, varargin{:});
Error in ==> dblquad>innerintegral at 84
Q(i) = quadf(intfcn, xmin, xmax, tol, trace, y(i), varargin{:});
Error in ==> quad at 77
y = f(x, varargin{:});
Error in ==> dblquad at 60
Q = quadf(#innerintegral, ymin, ymax, tol, trace, intfcn, ...
What am I doing wrong with that?
EDIT
Replacing
z=(x0-z0*tan(theta).*cos(phi))*(y0-z0*tan(theta).*sin(phi))*(z0/2)^4;
with
z=(x0-z0*tan(theta).*cos(phi)).*(y0-z0*tan(theta).*sin(phi))*(z0/2)^4;
I get an new error
??? Index exceeds matrix dimensions.
Error in ==> quad at 85
if ~isfinite(y(7))
Error in ==> dblquad>innerintegral at 84
Q(i) = quadf(intfcn, xmin, xmax, tol, trace, y(i), varargin{:});
Error in ==> quad at 77
y = f(x, varargin{:});
Error in ==> dblquad at 60
Q = quadf(#innerintegral, ymin, ymax, tol, trace, intfcn, ...
You need an element-wise operator
z=(x0-z0*tan(theta).*cos(phi)).*(y0-z0*tan(theta).*sin(phi))*(z0/2)^4;
^
|
As the help for dblquad says, the input x is a vector and y is a scalar and the output z is also vector. Thus anything in your integrand function that is a function of x will be a vector (e.g., rl) and you'll need to be careful to use element-wise operators where appropriate. You did not do this for the very last line.
Also, consider passing your initial value parameters via function handle rather than duplicating them inside the integrand function:
function dblquadtest
z0 = 20; x0 = 15; y0 = 20;
f = #(x,y)IntRect(x,y,x0,y0,z0);
Q = dblquad(f,0,x0/2,0,y0/2); % 1e-6 is the default tolerance
function z = IntRect(x, y, x0, y0, z0)
%The variables
rl=sqrt(x.^2+y.^2+(z0/2)^2);
theta=acos(z0./(2*rl));
phi=atan(y./x);
%The function
z=(x0-z0*tan(theta).*cos(phi)).*(y0-z0*tan(theta).*sin(phi))*(z0/2)^4;
z=z./rl.^3;
Related
I have such an question, And I will do it in matlab. But, I get some errors:
Find the value of x ∈ [0, 1] that minimizes the largest eigenvalue of the matrix A(x) = xM +(1−x)P, where M is a 5×5 magic square and P is a 5 × 5 Pascal matrix.
My matlab code:
%Define Matrices
M = magic(5);
P = pascal (5);
% Define the variable x
syms x
%Define the given matrix A
>> A = x*M + (1-x)*P;
%Define the eigenvalue lambda as y;
syms y
%Find determinant of |A - lambda * I|
D = det (A - y*eye(5))
%Define Objective function
objective = #(y) y
%And Define the constraint
constraint = #(x,y) (-1)*D
%initial value x0 = (0:0.001:1);
%Minimization problem solving
x = fmincon(objective, constraint, x0)
I get this error;
Error using fmincon (line 221)
FMINCON requires the following inputs to be of data type double: 'X0'.
Or If I use another function: fminsearch
x = fminsearch(objective, constraint, x0)
In this case I get the following error:
Error using fminsearch (line 96)
FMINSEARCH accepts inputs only of data type double.
How can I deal with these errors ? Where is my mistake? How can I correct them?
I guess what you are looking for might be fminbnd, which helps to
Find minimum of single-variable function on fixed interval
n = 5;
M = magic(n);
P = pascal(n);
x = fminbnd(#(x) max(eig(x*M + (1-x)*P)),0,1);
such that
>> x
x = 0.79603
I suspect you did not show us the proper code, as you have sums there. I suspect you mean syms.
fmincon only works for numeric data, not symbolic data.
I need to compute this kind of integral
I defined the two f(x,y) and g(x,y) inside a single function
h = {#(x,y) f, #(x,y) g};
where f and g are formulas defined before. In this way I have a 1x2 cell and I want to compute the scalar product as
F = #(x,y) dot(h,E)
where E is a vector 2x1 and then integrate
int = integral2(F,a,b,c,d);
giving me this error
Undefined function 'conj' for input arguments of type 'cell'.
Error in dot (line 37)
c = sum(conj(a).*b);
Error in tm_np>#(R,PHI)dot(e_n_even,E)
Error in integral2Calc>integral2t/tensor (line 228)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
I don't understand why is giving me this error. As result of the integral I should have a 2x1 vector
The problem is the generation of your data. integral2 accepts only functions that get #(x,y) as vector input and return the same size. in your case h is already not that, and F has no #(x,y) inputs (well, it has, but you decided not to pass them to h!!!).
% sample data
f=#(x,y)x.^2+y;
g=#(x,y)y.^2+x+3;
E=[3 1];
% Create auxiliary fucntion
mydot=#(x,y)(f(x,y).*E(1)+g(x,y).*E(2));
% work
int = integral2(mydot,0,3,1,5);
I have to solve a system of non-linear equations of the following function:
function eq = ts_7(A,B,C,D,E)
syms x
% dbstop if error
g = D * sin(C * atan( B*x - E * (B*x - atan(B*x)))) + A; % magic formula
eq5 = taylor(g, x, 'Order',1, 'ExpansionPoint',7) + 4296; % x^0
eq1 = taylor(g, x, 'Order',2, 'ExpansionPoint',7) - eq5 + 296.3; % x^1
eq2 = taylor(g, x, 'Order',3, 'ExpansionPoint',7) - eq1 - 79.77; % x^2
eq3 = taylor(g, x, 'Order',4, 'ExpansionPoint',7) - eq2 - 4.541; % x^3
eq4 = taylor(g, x, 'Order',5, 'ExpansionPoint',7) - eq3 - 0.03358; % x^4
eq{1} = matlabFunction(eq1) % syms to numeric function
eq{2} = matlabFunction(eq2)
eq{3} = matlabFunction(eq3)
eq{4} = matlabFunction(eq4)
eq{5} = matlabFunction(eq5)
end
and my main is the following
opts = optimoptions('fsolve','InitDamping',0.005,'Algorithm','levenberg-marquardt');
init = [-1.3, 1.4, 4000, 0.12, 9]; % starting points
tic
coeff = fsolve(#(x)ts_7(x(1), x(2), x(3), x(4), x(5)), init,opts);
toc
and the error I keep getting is
Undefined function or variable "fuser".
Error in fsolve (line 257)
if ~isempty( isoptimargdbl('FSOLVE', {'F','J'}, fuser, JAC) )
Error in script_7 (line 11)
coeff = fsolve(#(x)ts_7(x(1), x(2), x(3), x(4), x(5)), init,opts);
Error in run (line 96)
evalin('caller', [script ';']);
I don't know how to fix it. I also tried solving the same problem with vpasolve and solve. For solve, it takes too long and for vpasolve, I get the error that symbolic parameters are not allowed in a non-poly equation.
Is there a way to transform string into a function (not a handle function what matlabFunction is doing in this code)?
I am getting desperate because I don't want to rewrite everything by hand.
UPDATE: I tried solving 2-D system, example from http://nl.mathworks.com/help/optim/ug/fsolve.html. I get the same error, here is the screenshot.
Your function ts_7 needs to return a floating point vector, but eq is a cell array of function handles. You also don't need to call matlabFunction five time. Instead:
eq = matlabFunction([eq1;eq2;eq3;eq4;eq5]);
Now eq is a function handle that returns a 5-by-1 column vector of doubles. This needs to be evaluated numerically at the expansion point x so that the output of ts_7 is a floating point vector, for example:
eq = eq(1);
It's not clear to me if you're also optimizing x or if you know this.
There was something wrong with my MATLAB. I solved the problem by using another version of MATLAB.
I've tried this:
linefunca = #(xa,ya) aa*xa + ba*ya + ca;
figure(1)
imshow(Pica);
hold on;
ezplot(linefunca,[1,1072,1,712]);
But I'm returned with this error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ezplotfeval/applyfun (line 80)
z(i) = feval(f,x(i),y(i));
Error in ezplotfeval (line 65)
z = applyfun(x,y);
Error in ezplot>ezimplicit (line 257)
u = ezplotfeval(f, X, Y);
Error in ezplot (line 153)
hp = ezimplicit(cax, f{1}, vars, labels, args{:});
Error in ps3 (line 313)
ezplot(linefunca,[1,1072,1,712]);
aa,ba,ca are all known values (column vectors). The x and y limits are the size of the image that I'm working with. I'm trying to plot a set of epipolar lines. Any suggestions?
EDIT:
lt = length(aa);
linefunca = #(x,y,t) aa.*x(t) + ba.*y(t) + ca(t);
figure(1)
imshow(Pica);
hold on;
for t=1:lt
ezplot(#(x,y,t) linefunca(x,y,t),[1,lt]);
end
As far as I know, ezplot can not plot a series of lines like plot. A way to work around this would be to add a parameter k to the anonymous function, which is used to select the current line. You can then go through all lines in a for loop and plot them one-by-one.
Further: as it is stated on the ezplot help page, you have to use the array functions .*, ./ and .^ , so ezplot can use vectors to evaluate the function.
N = 5;
aa = rand(N,1); ba = rand(N,1); ca = rand(N,1);
linefunca = #(xa,ya,k) aa(k).*xa + ba(k).*ya + ca(k);
hold on
for k=1:N
ezplot(#(x,y)linefunca(x,y,k),[-5,5,-5,5]);
end
hold off
So I need to solve x''(t) = -x(t)^p with initial conditions x(0)= 0 and v(0) = x'(0) = v_o = 1.
The value of the parameter p is 1.
This is what I have:
function [t, velocity, x] = ode_oscilation(p)
y=[0;0;0];
% transform system to the canonical form
function y = oscilation_equation(x,p)
y=zeros(2,1);
y(1)=y(2);
y(2)=-(x)^p;
% to make matlab happy we need to return a column vector
% so we transpose (note the dot in .')
y=y.';
end
tspan=[0, 30]; % time interval of interest
[t,velocity,x] = ode45(#oscilation_equation, tspan, 1);
t = y(:,1);
xposition=y(:,3);
velocity=y(:,2);
end
and this is the error message I receive:
ode_oscillation(1)
Error using odearguments (line 91)
ODE_OSCILLATION/OSCILATION_EQUATION must return a
column vector.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0,
odeArgs, odeFcn, ...
Error in ode_oscillation (line 17)
[t,velocity,x] = ode45(#oscilation_equation, tspan,1);
There's a few things going wrong here. First, from help ode45:
ode45 Solve non-stiff differential equations, medium order method.
[TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0) with TSPAN = [T0 TFINAL] integrates
the system of differential equations y' = f(t,y) from time T0 to TFINAL
with initial conditions Y0.
Note that ode45 expects a function f(t,y), where size(t) == [1 1] for time and size(y) == [1 N] or [N 1] for solution values. Your oscilation_equation has the order of input arguments inverted, and you input a constant parameter p instead of time t.
Also, the initial conditions Y0 should have the same size as y; so size(y0) == [N 1] or [1 N]. You just have 1, which is clearly causing errors.
Also, your output arguments t, xposition and velocity will be completely ignored and erroneous, since y is not set as output argument from ode45, and most of all, their names do not correspond to ode_oscilation's output arguments. Also, their order of extracting from columns of y is incorrect.
So, in summary, change everything to this:
function [t, v, x] = ode_oscilation(p)
% initial values
y0 = [0 1];
% time interval of interest
tspan =[0 30];
% solve system
[t,y] = ode45(#(t,y) [y(2); -y(1)^p], tspan, y0);
% and return values of interest
x = y(:,1);
v = y(:,2);
end