MATLAB Help. Plugging in matrix of variables into existing function - matlab

I have an existing function of two variables (x,y) called discriminant defined in the following way:
discriminant = xSecondPart * ySecondPart - xySecondPart.^2;
Where xSecondPart and ySecondPart are the second partial derivatives of a function f. xySecondPart is the partial derivative with respect to x of the partial derivative with respect to y of the same function f.
I need to print out the values of discriminant at each value of x in the matrix xAns.
The below code is not working...
for idx = 1:numel(xAns)
disp(discriminant(xAns(idx)));
end
Hopefully someone can provide a solution. Thank you
Best...SL

If you define the function discriminant anonymously, like so:
descriminant = #(x) 24*x.^2 - 32;
Then all you have to do is type the following statement in the command line or function you're running:
D = discriminant(xAns)
If your function has been defined using the elementwise operator '.' wherever necessary, then the statement above will print out the discriminant function evaluated at every element of the matrix xAns, regardless of its size or shape. The values returned will be in the same shape as the matrix xAns. I think that would be the easiest way to solve your problem.

Related

how to evaluate to multivariable function

Consider the function f(y1,y2,y3,y4,z1)=(-(z1^3)/(y3^2))(3(y2-y1+y3^(-1)-z1/10)^2+(1/5)(y2-y1+y3^(-1)-(z1)/10))-y4 with 5 variables. When I put f(1,1,1,1,1) I find the answer but when I write f(v) in which v=(1,1,1,1,1) matlab does not work. How can I do that (by the second way)?
You could write a wrapper function, that will take a vector as input argument and 'unpacks' this vector before calling your function.
f(y1,y2,y3,y4,z1) = (-(z1^3)/ ...; % your function
fv(v) = f(v(1),v(2),v(3),v(4),v(5));
Or alternatively, make f refer to elements in the input vector by using the appropriate indices:
f(v) = (-(v(5)^3)/(v(3)^2)); ... % etc.

Using `lsqnonlin` with vector inputs

I have a question about using the lsqnonlin function.
In my case I have two functions:
f_1=#(t,x)sin(t+x.^2);
f_2=#(t,x)cos(x.^2)+3.*t.^2;
f = {f_1, f_2};
I want to find the values of the arguments t and x which would result in the least square error, defined as: f_1(t,x)^2+f_2(t,x)^2. In other words, argmin for LSE.
My code is as follow with initial guess [1,2]:
lsqnonlin(f,[1,2])
And I'm getting the error:
Error in lsqnonlin (line 196)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Caused by:
Failure in initial objective function evaluation. LSQNONLIN cannot continue.
lsqnonlin can be used for vector function and vector input according to the documentation. I wonder how to prepare corresponding codes for it. Could anyone suggest the solution?
You are getting an error because lsqnonlin expects a scalar function handle that maps a vector to a vector, whereas you specify a cell array of function handles. To fix this, instead of a vector of functions outputting one scalar each, you need to rewrite it into a single function that accepts a vector of inputs and also outputs a vector:
f = #(xt)[sin(xt(2)+xt(1).^2), cos(xt(1).^2)+3.*xt(2).^2];
% xt = [x,t]
% f = [f_1(xt), f_2(xt)]
so f and xt are both vectors of 2 elements.
Then, the solver works:
lsqnonlin(f,[1,2])
ans =
1.6144 0.5354

Quadtree decomposition in MATLAB: qtdecomp function input

Does anyone have experience with defining an input function as threshold criterion for the qtdecomp function in MATLAB? I tried the following but it did not work:
MyFunc = #(A,threshold) std2(A)>threshold;
S = qtdecomp(Image,MyFunc,threshold);
Somehow, for some threshold values, only the leftmost quarter of the quadtree is divided into new pieces. Could this maybe be an error in the qtdecomp code itself or is there something wrong with my function input?
See the attached image for details. I get this regardless of the threshold I choose:
The problem is that the image is passed to your anonymous function as an m x m x k array representing the image decomposed into k blocks. The function must return a vector of length k, but std2 only looks at the first block and returns a scalar. I'm still trying to come up with a vectorized approach to this, but for now here's a simple loop in a standalone function:
function v = Std2Func(A, threshold)
s = size(A,3);
v = zeros(1,s);
for k = 1:s
v(k) = std2(A(:,:,k))>threshold;
end
end
This iterates through the k planes of the input array, applying std2 to each 2d plane and putting the results into the output vector. Then you just call qtdecomp using a handle to the new function:
S = qtdecomp(Image,#Std2Func,threshold);

Matlab - how to build functions

Matlab has the function Legendre that returns to an integer m and real number r a vector of length m+1.
Now I want to define another function fun that gives me only the first component of this vector per fixed m, so I want to define a function fun(m,r) that gives me the first component of the vector legendre(m,x). The point is that fun(m,r) should also be a function, just like legendre. Does anybody know how to do this?
Define the function as follows:
function out = fun(n,x)
temp = legendre(n,x); %// store output of "legendre" in a temporary variable
out = temp(1); %// return only desired element
Of course, this should be placed in a file fun.m within Matlab's path.
Alternatively, if you are feeling hackish, you can use
getfield(legendre(n,x), {1})
to extract the first element of legendre(n,x) directly (without a temporary variable). This allows defining fun as an anonymous function as follows:
fun = #(n,x) getfield(legendre(n,x), {1});

MATLAB Function (Solving an Error)

I have one file with the following code:
function fx=ff(x)
fx=x;
I have another file with the following code:
function g = LaplaceTransform(s,N)
g = ff(x)*exp(-s*x);
a=0;
b=1;
If=0;
h=(b-a)/N;
If=If+g(a)*h/2+g(b)*h/2;
for i=1:(N-1)
If=If+g(a+h*i)*h;
end;
If
Whenever I run the second file, I get the following error:
Undefined function or variable 'x'.
What I am trying to do is integrate the function g between 0 and 1 using trapezoidal approximations. However, I am unsure how to deal with x and that is clearly causing problems as can be seen with the error.
Any help would be great. Thanks.
Looks like what you're trying to do is create a function in the variable g. That is, you want the first line to mean,
"Let g(x) be a function that is calculated like this: ff(x)*exp(-s*x)",
rather than
"calculate the value of ff(x)*exp(-s*x) and put the result in g".
Solution
You can create a subfunction for this
function result = g(x)
result = ff(x) * exp(-s * x);
end
Or you can create an anonymous function
g = #(x) ff(x) * exp(-s * x);
Then you can use g(a), g(b), etc to calculate what you want.
You can also use the TRAPZ function to perform trapezoidal numerical integration. Here is an example:
%# parameters
a = 0; b = 1;
N = 100; s = 1;
f = #(x) x;
%# integration
X = linspace(a,b,N);
Y = f(X).*exp(-s*X);
If = trapz(X,Y) %# value returned: 0.26423
%# plot
area(X,Y, 'FaceColor',[.5 .8 .9], 'EdgeColor','b', 'LineWidth',2)
grid on, set(gca, 'Layer','top', 'XLim',[a-0.5 b+0.5])
title('$\int_0^1 f(x) e^{-sx} \,dx$', 'Interpreter','latex', 'FontSize',14)
The error message here is about as self-explanatory as it gets. You aren't defining a variable called x, so when you reference it on the first line of your function, MATLAB doesn't know what to use. You need to either define it in the function before referencing it, pass it into the function, or define it somewhere further up the stack so that it will be accessible when you call LaplaceTransform.
Since you're trying to numerically integrate with respect to x, I'm guessing you want x to take on values evenly spaced on your domain [0,1]. You could accomplish this using e.g.
x = linspace(a,b,N);
EDIT: There are a couple of other problems here: first, when you define g, you need to use .* instead of * to multiply the elements in the arrays (by default MATLAB interprets multiplication as matrix multiplication). Second, your calls g(a) and g(b) are treating g as a function instead of as an array of function values. This is something that takes some getting used to in MATLAB; instead of g(a), you really want the first element of the vector g, which is given by g(1). Similarly, instead of g(b), you want the last element of g, which is given by g(length(g)) or g(end). If this doesn't make sense, I'd suggest looking at a basic MATLAB tutorial to get a handle on how vectors and functions are used.