Using a function that has another function as parameter: - matlab

I want to integrate x^2 from 2 to 4 with the trapezoidal integration method. For this, I defined a function trap that takes 4 arguments:
function y = trap( fn, a, b, h )
n = (b-a)/h;
x = a + [1:n-1]*h;
y = h/2*(feval(fn, a) + feval(fn, b) + 2*sum(feval(fn,x)));
and a function f
function y= f(x)
y=x^2
end
Now, by executing trap(f,2,4,0.1), I get the following error:
Not enough input arguments.
Error in f (line 2)
y=x^2
What is the origin of that error?

You have to call trap using the function handle #f, not f.
trap(#f,2,4,0.1)
function y = trap( fn, a, b, h )
n = (b-a)/h;
x = a + [1:n-1]*h;
y = h/2*(fn(a) + fn(b) + 2*sum(fn(x)));
end
function y= f(x)
y = x.^2;
end
which gives, as expected,
ans =
18.67
Also you needed element-wise multiplication in f(x) to compute y = x.^2.
And feval is not necessary. You can directly call fn(a) to evaluate the function.

Related

Adding function handlers in cell array in MATLAB

I have a bunch of anonymous functions stored in a cell array as follows:
F = {#(x) x + 1, #(x) x * x}
I want to create a new anonymous function to add them all up and average the result given an input x. F can have arbitrary number of function handles and is generated at run time. If F is known, then it is simply f = #(x) (F{1}(x) + F{2}(x)) / length(F). But I don't know how to append all elements of F onto this new anonymous function (presumably in a loop?) How would I do this?
Use cellfun to define a function that evaluates each function f in F using just one line. An anonymous function handle for arbitrary F and x is as follows:
F = {#(x) x + 1, #(x) x * x};
%// Build anonymous function that evaluates each function, sums,
%// divides by length of F
new_F = #(x,F)sum(cellfun(#(f)f(x), F)) / length(F);
Then, to evaluate, simply call:
x = 2; %// data to apply fcns on
result = new_F(x, F)

How to write/code several functions as one

I am trying to write a line composed of two segments as a single equation in :
y = m1*x + c1 , for x<=x1
y = m2*x + c2 , for x>=x1
My questions are:
How can I write the function of this combined line as a single equation?
How can I write multiple functions (valid in separate regions of a linear parameter space) as a single equation?
Please explain both how to express this mathematically and how to program this in general and in Matlab specifically.
You can write this equation as a single line by using the Heaviside step function, https://en.wikipedia.org/wiki/Heaviside_step_function.
Combining two functions into one:
In fact, what you are trying to do is
f(x) = a(x) (for x < x1)
f(x) = q (for x = x1), where q = a(x1) = b(x1)
f(x) = b(x) (for x > x1)
The (half-maximum) Heaviside function is defined as
H(x) = 0 (for x < 0)
H(x) = 0.5 (for x = 0)
H(x) = 1 (for x > 0)
Hence, your function will be
f(x) = H(x1-x) * a(c) + H(x-x1) * b(x)
and, therefore,
f(x) = H(x1-x) * (m1*x+c1) + H(x-x1) * (m2x+c2)
If you want to implement this, note that many programming languages will allow you to write something like
f(x) = (x<x1)?a(x):b(x)
which means if x<x1, then return value a(x), else return b(x), or in your case:
f(x) = (x<x1)?(m1*x+c1):(m2x+c2)
Matlab implementation:
In Matlab, you can write simple functions such as
a = #(x) m1.*x+c1,
b = #(x) m2.*x+c2,
assuming that you have previously defined m1, m2, and c1, c2.
There are several ways to using/implementing the Heaviside function
If you have the Symbolic Math Toolbox for Matlab, you can directly use heaviside() as a function.
#AndrasDeak (see comments below) pointed out that you can write your own half-maximum Heaviside function H in Matlab by entering
iif = #(varargin) varargin{2 * find([varargin{1:2:end}], 1, 'first')}();
H = #(x) iif(x<0,0,x>0,1,true,0.5);
If you want a continuous function that approximates the Heaviside function, you can use a logistic function H defined as
H = #(x) 1./(1+exp(-100.*x));
Independently of your implementation of the Heaviside function H, you can, create a one-liner in the following way (I am using x1=0 for simplicity) :
a = #(x) 2.*x + 3;
b = #(x) -1.5.*x + 3;
Which allows you to write your original function as a one-liner:
f = #(x) H(-x).*a(x) + H(x).*b(x);
You can then plot this function, for example from -10 to 10 by writing plot(-10:10, f(-10:10)) you will get the plot below.
Generalization:
Imagine you have
f(x) = a(x) (for x < x1)
f(x) = q (for x = x1), where q = a(x1) = b(x1)
f(x) = b(x) (for x1 < x < x2)
f(x) = r (for x = x2), where r = b(x2) = c(x2)
f(x) = c(x) (for x2 < x < x3)
f(x) = s (for x = x2), where s = c(x3) = d(x3)
f(x) = d(x) (for x3 < x)
By multiplying Heaviside functions, you can now determine zones where specific functions will be computed.
f(x) = H(x1-x)*a(c) + H(x-x1)*H(x2-x)*b(x) + H(x-x2)*H(x3-x)*c(x) + H(x-x3)*d(x)
PS: just realized that one of the comments above talks about the Heaviside function, too. Kudos to #AndrasDeak .

Using a function handle with equation and Simpson's rule

I'm working on a problem that applies a luminosity equation:
E = 64.77* T^−4 ∫ x^−5( e^(1.432/Tx) -1 )^−1 dx
Where T = 3500;
to simp son's rule which is a few sums and such.
problem 17.8 here: http://my.safaribooksonline.com/book/computer-aided-engineering/9780123748836/-introduction-to-numerical-methods/ch17lev1sec10
What I've done is made a function simpson(fn, a, b, h) that runs simp son's rule correctly.
however, the problem is making that integral equation into a function handle that works. I've gotten it to work for simple function handles like
f = #x x.^2
but when I try and make the integral into a function:
fn = #(x)(64.77/T^4).*integral((x.^(-5)).*((exp(((1.432)./(3500.*x)))).^(-1)), 4e-5, 7e-5);
simp(fn, 5, 15, 1)
function s = simp(fn, a, b, h)
x1 = a + 2*h:2*h:b-2*h;
sum1 = sum(feval(fn, x1));
x2 = a + h:22*h:b-h;
sum2 = sum(feval(fn, x2));
s = h/3*(feval(fn, a) + feval(fn, b) + 4*sum2 + 2*sum1);
it doesn't work. error message is Integral: first input must be function handle.
Any help appreciated.
You're supposed to be evaluating the integral using Simpsons rule, whereas you are using integral to calculate the integral, then fn is not a function of x. You want to do this:
fn = #(x)(x.^(-5)).*((exp(((1.432)./(3500.*x)))).^(-1));
I=simp(fn,a,b,h);
E=(64.77/T^4)*I;

Sending a function into a matlab function

I'm trying build a matlab function that will evaluate a function and vector that are sent in as parameters. I'm having a hard time trying to figure out how to send in the function so that it can be evaluated in the matlab function. I figured out how to do it without the function but I'm a little lost trying to evaluate it within a matlab function. Below is my code...
This is what I'm trying to do...
x = [x1 x2]';
f = x(x1)^2 + 2 * (x2)^2
x = [5 10];
f = (5)^2 + 2 * (10)^2 % which I would like to return 225, not a column vector
This is what I have and what I have tried...
x = [5 10]';
% without using a matlab function
% k = 1
% f = x(k)^2 + 2 * x(k + 1)^2; % returns the correct answer of 225
f = x^2 + 2 * x^2 % complains about the scalar 2
f = x.^2 + 2 * x.^2 % returns a column vector [75; 300]
function [value] = evalFunction(f,x)
value = f(x);
I've tried...
f = #(x) x.^2 + 2 * (x+1).^2;
value = evalFunction(#f,x) %Error: "f" was previously used as a variable
So I tried...
f = #(x) x.^2 + 2 * (x+1).^2;
value = evalFunction(f,x) %value = [97;342]
I'm new to matlab so any help is appreciated. I've been doing some research and found some stuff here on stackoverflow but can't seem to get it to work. I've seen there are other ways to do this, but I will eventually be adding more code to the matlab evalFunction function so I'd like to do it this way. Thanks!
Anonymous functions and function handles plus array indexing. Taking x as a 2-element vector, define and use your function like:
f = #(x) x(1).^2 + 2 * x(2).^2;
value = evalFunction(f,x) % but you can just do f(x) if that is all you need
However, if evalFunction does nothing other than evaluate f at x, then you don't need it at all. Just do f(x).
Alternately,
f = #(x1,x2) x1.^2 + 2*x2.^2;
value = evalFunction(f,x1,x2); % here your function will call it by f(x1,x2)
You are probably coming at this from a C background - in Matlab, x+1 is the entire vector x with 1 added - not the element offset by 1.
The function you need is
f = #(x)x(1).^2 + 2 * (x(2)).^2;
or, to be a little more "matlab-like":
f = #(x) [1 2] * x(1:2)'.^2;
Which performs the element-wise square of the first two elements of x as a column vector, and then does the matrix multiplication with [1 2], resulting in
1 * x(1) .^2 + 2 * x(2) .^2;
Which seems to be what you were asking for.
caveat: did not have opportunity to test this...

Passing matrix to function handle (MATLAB)

If I have a set of functions
f = #(x1,x2) ([x1 + x2; x1^2 + x2^2])
and I have a second matrix with
b = [x1,x2]
How do I evaluate f([b])? The only way I know how is to say f(b(1),b(2)) but I can't figure out how to automate that because the amount of variables could be up to n. I'm also wondering if there is a better way than going individually and plugging those in.
convertToAcceptArray.m:
function f = convertToAcceptArray(old_f)
function r = new_f(X)
X = num2cell(X);
r = old_f(X{:});
end
f = #new_f
end
usage.m:
f = #(x1,x2) ([x1 + x2; x1^2 + x2^2])
f2 = convertToAcceptArray(f);
f2([1 5])
You could rewrite your functions to take a vector as an input.
f = #(b)[b(1) + b(2); b(1)^2 + b(2)^2]
Then with, e.g., b=[2 3] the call f(b) gives [2+3; 2^2+3^2]=[5; 13].
Assuming that b is an N-by-2 matrix, you can invoke f for every pair of values in b as follows:
cell2mat(arrayfun(f, b(:, 1), b(:, 2), 'UniformOutput', 0)')'
The result would also be an N-by-2 matrix.
Alternatively, if you are allowed to modify f, you can redefine it to accept a vector as input so that you can obtain the entire result by simply calling f(b):
f = #(x)[sum(x, 2), sum(x .^ 2, 2)]