Nested integral within integral2 in matlab - matlab

I'm attempting to take the double integral (using integral2) of a function that is defined by an integral.
http://i.imgur.com/gIUsLSw.jpg
Here is what I am currently attempting:
t=linspace(0,1,50);
fun_1= #(v) exp(.071*v)
fun = #(x,y) exp(0.14*0.00607*integral(#(u)fun_1(u),0,x)).*exp(-(x-y).^2).*exp(0.14*0.00607*integral(#(u)fun_1(u),0,x));
for i=2:length(t)
for j=i:length(t)
A(i,j)=integral2(fun,t(i-1),t(i),t(j-1),t(j));
end
end
I'm receiving the error
Error using integral (line 86) A and B must be floating point scalars.
Can anyone provide any information on how to fix this problem.

Here you go:
l=3;
t=linspace(0,1,365);
fun3= #(v) integral(#(v)exp(.071*v),0,v,'ArrayValued',true);
for i=2:length(t)
for j=i:length(t)
xx=t(i);
yy=t(j);
fun = #(x,y) exp(0.14*0.00607*fun3(yy)).*exp(-(x-y).^2/l).*exp(0.14*0.00607*fun3(xx));
y(i,j)=integral2(fun,t(i-1),t(i),t(j-1),t(j));
end
end
It works, but it is very slow.

Related

Computing the Jacobian of an anonymous function - MATLAB

I'm trying to solve a system of non linear odes in Matlab as follows.
editparams %parameters
Tend = 50;
Nt = 100;
% Define RHS functions
RHS = #(t,x) ModelRHS(t,x,param); %anonymous function defining the set of equations
%Execution-----------------------------------------------------------------
x0 =[0.04;0.75;0.85]; %Initial condition
t = linspace(0,Tend,Nt); %TSPAN
[t x] = ode45(RHS, t, x0);
Now, I need to find the steady state of the system and I'm trying to create a function for this. I thought I'd calculate the steady state using the Jacobian. My equations are in an anonymous function which is defined as f in the code below. However, I realised that jacobian does not work for anonymous functions (or may be there is a way to do with anonymous functions) . so I thought I would convert the anonymous function to a symbolic function and try it. But
i still have a difficulty in getting that done. So any help is really appreciated!
function SS = SteadyState(param, E)
f = #(t,x)ModelRHS(t,x,param,E); %set of odes
SymbolicSystem = sym(f); %trying to make the anonymous function symbolic
SymbolicJacobian = jacobian(SymbolicSystem',x); %jacobian
Jacob = matlabFunction(SymbolicJacobian,x);
end
Also if there is any other way apart from finding the Jacobian, kindly let me know about that too.
I tried using 'fsolve' to calculate the steady-state as follows:
f = #(t,x)ModelRHS(t,x,param);
x0 =[0.04;0.75;0.85]';
options = optimoptions('fsolve','Display','iter'); % Option to display output
SS = fsolve(f,x0,options); % Call solver
but it returned an error
Not enough input arguments.
Error in #(t,x)ModelRHS(t,x,param)
Error in fsolve (line 242)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.

fzero() Matlab function for complicated functions

Having such a function:
y=1.2*sin(x)+2*log(x+2)-5; I am looking for zeros of that function using fzero() functon- just for testing, I indicate other methods.
I received error and I am looking for the solution of that. fzero() is for nonlinear functions but for complex ones...? Doyou know similar method to fzero()?
The function in the example has a pole, but you can treat this case by looking at it's real part, get the zero and check it to see the imaginary part is zero:
syms x y yr
yr= #(x) real(1.2*sin(x)+2*log(x+2)-5);
fr=fzero(yr,0);
fr =
6.8458
y= #(x) (1.2*sin(x)+2*log(x+2)-5);
y(fr)
ans =
-8.8818e-16

Double integral of objective function

I want to evaluate the double integral of my objective function (named myfunction, see below) using the build-in-function integral2.
I have tried to run this script;
f = #(r,theta) myfunction(r,theta);
integral2(f,0,2,0,2*pi);
where myfunction is the following function:
function fkt=myfunction(r,theta)
x=r.*cos(theta);
y=r.*sin(theta);
los(:,1)=x;
los(:,2)=y;
norm = (sum( sqrt( los(:,1).^2 + los(:,2).^2)));
fkt=norm*r;
end
I am making the integral in polar coordinates, that why fkt=norm*r.
Matlab gives me the following error message:
>> untitled2
Subscripted assignment dimension mismatch.
Error in myfunction (line 8)
los(:,1)=x;
I can't figure out, what the problem is.
There are two things that can be improved:
los is undefined
los(:,1) is a column while x is a row, so the assignment has to fail.
You can correct this by defining los and change your assignment. For instance:
los = NaN(numel(r), 2);
los(:,1) = x';
los(:,2) = y';
But, why do you need the variable los? Just remove it and the error will be gone:
function fkt=myfunction(r,theta)
x=r.*cos(theta);
y=r.*sin(theta);
norm = (sum( sqrt(x.^2 + y.^2)));
fkt=norm*r;
end
Best,
x is a matrix and you try to assign it to a column vector.

Eigenvalues calculations in Matlab?

I am going calculate the eigenvalues follow program:
I want to calculate the matrix H , and achieve eigenvalues.
acc=1.44e-10;
a=1.732*acc;
t=-2.550;
x1=0;
y1=0;
z1=0;
x2=acc*cos(60);
y2=acc*sin(60);
z2=0;
sh=0;
for i=-1:1
for j=-1:1
for k=1:2
sh=sh+1;
xk=acc*cos(60)*(k-1);
yk=acc*sin(60)*(k-1);
zk=0;
xx(sh)=(i*a)+(j*a/2)+xk;
yy(sh)=(sqrt(3)/2)*a*j+yk;
zz(sh)=zk;
ki(sh)=k;
R1(sh)=i;
R2(sh)=j;
end
end
end
L0=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2);
r1=0:0.02:1;
kx=(2*pi/(sqrt(3)*a))*(1-r1);
ky=(2*pi/(3*a))*(1-r1);
for ik=1:50
for i=1:2
for j=1:sh
Dis(i+8,j)=sqrt((xx(i)-xx(j))^2+(yy(i)-yy(j))^2+(zz(i)-zz(j))^2);
if abs(Dis-L0)<0.1
Rx=R1(sh)*a+R2(sh)*a/2;
Ry=R2(sh)*sqrt(3)/2*a;
H(ki(i+8),ki(j))=H(ki(i+8),ki(j))+t*exp(1i*(kx(ik)*Rx+ky(ik)*Ry));
end
end
end
end
But I always get this error:
Error in (line 44)
H(ki(i+8),ki(j))=H(ki(i+8),ki(j))+t*exp(1i*(kx(ik)*Rx+ky(ik)*Ry));
Undefined function 'H' for input arguments of type 'double'.
How to solve this error?
If you initialize H in the beginning, the code works.
H = zeros(2,2);
Notice:
I do not know the way you are calculating the eigenvalues. You should use the predefined function eig to check your solution. Another ways is to use SVD to calculate the eigenvectors and eigenvalues.

why isn't the result a scalar?

i'm stuck with this error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
yres(1)=((u - uc).^2) + ((y - yc).^2) -(d.^2);
i don't understand, why this won't get a skalar?since the elements are all scalar. what should be changed to get a scalar?
best regards
edit: thanks sloede, all inputs are scalar, but i still get this error
In an assignment A(I) = B, the number of elements in B and I must be the
same.
Error in myfun (line 7)
yres(1)=sqrt(((u - uc).^2) + ((y - yc).^2) ) -d;
Error in fsolve (line 241)
fuser = feval(funfcn{3},x,varargin{:});
Error in modfsolve (line 26)
x= fsolve(#myfun,x0,options,uc,d,spacing_amplitude,spacing_width);
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE
cannot continue.*
The "." before an operator means that the following operation should be applied element-wise and not on the vector as a whole. Thus
a = b.^2
will give you as a result all elements of b squared and saved back to a. Therefore, in your code statement above, if any of u, uc, y, yc, d are not scalar but a vector, your result will be a vector as well.
Otherwise there seems to be nothing wrong with your code.
read the documentation of fsolve: http://www.mathworks.nl/help/toolbox/optim/ug/fsolve.html
it states:
fun
The nonlinear system of equations to solve. fun is a function that accepts a vector x and returns a vector F, the nonlinear equations evaluated at x.
Obviously your function myfun doesn't handle vector input.
You can solve this by adding the following construction inside your function (and of course change it to your needs/your parameters):
function out = myfun(in)
if ~isscalar(in)
% assuming it's a matrix or vector
out = reshape(arrayfun(#myfun,in(:)),size(in));
else
% your actual function execution statements
out = dostuffon(in);
end
end
or properly vectorize your function (if that's possible)