Double integral of objective function - matlab

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.

Related

Trying to implement Richardson's Extrapolation - Basic Syntax Assistance

I so far have the following
y = log(x);
% Ask user for input values for h and M
% M denotes the number of steps of the algorithm.
h = input('Input value h: ');
M = input('Input value M: ');
%Initialize an MxM matrix
D = zeros(M);
phi = (1/(2*h)) * (y(x+h) - y(x-h));
print(phi);
I obtain the error
Error using symengine (line 58) Index exceeds matrix dimensions.
Error in sym/subsref (line 696)
B = mupadmex('symobj::subsref',A.s,inds{:});
Error in RE (line 12) phi = (1/(2*h)) * (y(x+h) - y(x-h));
First, I believe I should be getting an error message about x not being defined. Second, I have no idea what the matrix dimension error is about. Third, and most importantly, how can I declare the function phi so that it becomes what I wrote?
First, I believe I should be getting an error message about x not being defined.
I'm guessing that x is defined, or you would get that error upon the line defining phi. To check whether x is defined, type "who" or "whos".
Second, I have no idea what the matrix dimension error is about.
This is most likely because y is a scalar, x + h is equal to some nonzero integer that is not 1, and you're trying to access y(x + h). For your own edification try setting y equal to a scalar (e.g. y = 5;) and seeing what errors are produced by indexing it in various legitimate and non-legitimate ways (e.g. y(1), y(0), y(3), y(-1), y(1.5)).
Third, and most importantly, how can I declare the function phi so that it becomes what I wrote?
Based on the context it looks like you want y to be defined as a function of x instead of a scalar. In other words:
y = #(x)log(x);
phi = (1/(2*h)) * (y(x+h) - y(x-h));
The code runs without error when you change the definitions to the above.
One other error you will run into: the print command is not what you're looking for - this prints a figure to a file. You're probably looking for:
disp(phi);

Bessel's integral implementation

I'm trying to implement this integral representation of Bessel function of the first kind of order n.
here is what I tried:
t = -pi:0.1:pi;
n = 1;
x = 0:5:20;
A(t) = exp(sqrt(-1)*(n*t-x*sin(t)));
B(t) = integral(A(t),-pi,pi);
plot(A(t),x)
the plot i'm trying to get is as shown in the wikipedia page.
it said:
Error using * Inner matrix dimensions must agree.
Error in besselfn (line 8) A(t) = exp(sqrt(-1)*(n*t-x*sin(t)));
so i tried putting x-5;
and the output was:
Subscript indices must either be real positive integers or logicals.
Error in besselfn (line 8) A(t) = exp(sqrt(-1)*(n*t-x*sin(t)));
How to get this correct? what am I missing?
To present an anonymous function in MATLAB you can use (NOT A(t)=...)
A = #(t) exp(sqrt(-1)*(n*t-x.*sin(t)));
with element-by-element operations (here I used .*).
Additional comments:
You can use 1i instead of sqrt(-1).
B(t) cannot be the function of the t argument, because t is the internal variable for integration.
There are two independent variables in plot(A(t),x). Thus you can display plot just if t and x have the same size. May be you meant something like this plot(x,A(x)) to display the function A(x) or plot(A(x),x) to display the inverse function of A(x).
Finally you code can be like this:
n = 1;
x = 0:.1:20;
A = #(x,t) exp(sqrt(-1)*(n*t-x.*sin(t)));
B = #(x) integral(#(t) A(x,t),-pi,pi);
for n_x=1:length(x)
B_x(n_x) = B(x(n_x));
end
plot(x,real(B_x))

Nonlinear parameters search

need to find a set of optimal parameters P of the system y = P(1)*exp(-P(2)*x) - P(3)*x where x and y are experimental values. I defined my function
f = #(P) P(1)*exp(-P(2)*x) - P(3)*x
and
guess = [1, 1, 1]
and tried
P = fminsearch(f,guess)
according to Help. I get an error
Subscripted assignment dimension mismatch.
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
I don't quite understand where my y values would fall in, as well as where the function takes P from. I unfortunately have no access to nlinfit or optimization toolboxes.
You should try the matlab function lsqnonlin(#testfun,[1;1;1])
But first make a function and save in an m-file that includes all the data points, lets say your y is A and x is x like here below:
function F = testfun(P)
A = [1;2;3;7;30;100];
x = [1;2;3;4;5;6];
F = A-P(1)*exp(-P(2)*x) - P(3)*x;
This minimized the 2-norm end gives you the best parameters.

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)

Putting Two Arguments into UnifDist in MATLAB

Trying to compute the following expression,
quad(#(n)quad(#(m)unifpdf(m-n,0,1),-10,10),-10,10)
But I get a message saying:
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> #(m)unifpdf(m-n,0,1)
Can you please let me know how to fix this?
The problem here is the following requirement:
The function y = fun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x.
unipdf seems to satisfy this requirement, but quad itself is not.
To fix this problem you need to write a wrapper function that accepts a vector argument x, evaluates inner integral using quad function, and return a vector of results:
function [r] = Test()
r = quad(#(n)InnerIntegral(n),-10,10);
end
function [y] = InnerIntegral(n)
y = zeros(size(n));
for i = 1 : length(n)
y(i) = quad(#(m)unifpdf(m - n(i), 0, 1), -10, 10);
end;
end