Integral piecewise function matlab - matlab

I am trying to integrate a function F which is defined as:
function F
x = -3:0.1:3;
F = zeros(1, length(x));
for i = 1:length(x)
if (1.4<= x(i)) && (x(i) <= 1.6)
F(i) = x(i).^2;
else
F(i) = 2;
end
end
end
but the integral function gives me an error saying that there are too many arguments. I think the problem that the function is defined as a points?

The problem with your function, is that integral has no way to pass the arguments you supply to your function F. The function doesn't know that it can just pull certain elements from the vector you created. If you rewrite your function such that for an input (or x value), the output of F is returned, then integral will work as you need given two values to integrate between.

Related

Matlab integrate of fitted "linearinterp" returning error "First input argument must be a function handle"

Trying to get the integral of some experimentally collected data.
After using the envelope and abs functions I'm using the fit function to get the equation I wish to integrate (unfortunately 'poly' isn't giving a close enough fit to the data):
[yupper,ylower] = envelope(signal,1000,'peak');
dat = abs(yupper);
f = fit(x,dat,'linearinterp');
Then when I try
q = integral(f,3e4,9e4);
I get the error:
Error using integral (line 82) First input argument must be a function
handle.
Error in findenergyfromfitcurve (line 10) q = integral(f,3e4,9e4);
I thought f was a (mathematical) function, don't understand what the error is telling me. When I try using 'poly3' incase it's the linearinterp messing things up I still get that error.
TIA
f is a function but its type is cfit not function handle.
integral() function requires function handle, what you can do
is transform cfit into function handle before taking the
integral
The code is as follows
x = rand(5,1);
dat = rand(5,1);
f = fit(x,dat,'linearinterp');
% Create a new function handle
f = #(x)f(x);
q = integral(f, 3e4,9e4, 'ArrayValued', 1)
2) What does the ... 'Array valued', 1) do as well? It didn't work
till I put that in so it must do something
f is a piecewise function, the following illustration is based on the assumption that f is a 2-piecewise linear function, but it can be used for n-piecewise function as well.
The task for fit() function is finding the parameters :
a
b
c
d
k
In terms of code f looks like
function y = f(x,a,b,c,d,k)
% PIECEWISELINE A line made of two pieces
% that is not continuous.
y = zeros(size(x));
% This example includes a for-loop and if statement
% purely for example purposes.
for i = 1:length(x)
if x(i) < k
y(i) = a.* x(i) + b;
else
y(i) = c.* x(i) + d;
end
end
end
To plot a function handle, just use fplot(f)
Here is the graph for f
To sum up, f probably has more than one expression, that's why I set
'ArrayValued' to true so that integral() function knowns f
has more than one expression, omitting it means f has a single
expression which is not true.

Integral in Matlab

I have 3 equations:
f = (exp(-x.^2)).*(log(x)).^2
g = exp(-x.^2)
h = (log(x)).^2
The interval is:
x = 0.05:10
I am able to correctly plot the equations but when I try to find an integral, it says that there is an error.
The code I used to find an integral is:
integral(f,0,Inf)
integral(g,0,inf)
integral(h,0,10)
The integrals for f and g are from 0 to infinity and the integral for h is from 0 to 10. None of my code to find integrals works.
You need to define f,g,h as functions like shown below. See documentation of integral(), it takes a function as its first argument. Matlab integral documentation
x = 0.05:10
f = #(x) (exp(-x.^2)).*(log(x)).^2
g = #(x) exp(-x.^2)
h = #(x) (log(x)).^2
integral(f,0,Inf) % 1.9475
integral(g,0,inf) % 0.8862
integral(h,0,10) % 26.9673
h = #(x) (log(x)).^2
This syntax is called anonymous functions, basically they are nameless functions. In above case it takes x as input and returns log(x) squared.
From now on h is a function and it can be used like this.
h(1) % will be equal 0
For more on anonymous functions refer to matlab anonymous functions guide:
Anonymous Functions

Matlab function that returns gradient

I'm trying to create a function, that has two output arguments:
1. The calculated f(x) value
2. The gradient
But it's calling itself recursively all the time.
What am I doing wrong?
function [y, gra] = f1(x)
y = x^2
syms z
gra = gradient(f1(z))
Thanks.
edit:
Now I have this:
function [y, gra] = f1(x)
y = x^2
if nargout == 2
syms x
gra = gradient(f1(x))
end
edit 2:
I'm trying to use the function in the following:
[y, grad] = f1(5);
y_derived = grad(10);
I think this is what you want to do:
function [y, gra] = f1(x)
f=#(x) x^2;
y=f(x); %// calculate y
syms z %// initialise symbolic variable
gra=gradient(f(z),z); %// symbolic differentiation
This will return g as a symbolic function. To calculate a value, you can use subs(gra,z,123), or, if you are going to evaluate it many times, do gradFunc=matlabFunction(gra) then gradFunc(v) where v is a vector or matrix of points you want to evaluate.
That's because the argument into gradient is your function name f1(z). As such, it keeps calling f1 when your original function is also called f1, and so the function keeps calling itself until you hit a recursion limit.
I think you meant to put gradient(y) instead. Try replacing your gradient call so that it is doing:
gra = gradient(y);

How to calculate the integral of the exp of the sum of function handles in a cell

I don't know how to calculate the integral of the sum of function handles in a cell. Please see the below examples:
f{1} = #(x) x;
f{2} = #(x) x^2;
g = #(x) sum(cellfun(#(y) y(x), f));
integral(#(x) exp(g), -3,3);
Error: Input function must return 'double' or 'single' values. Found 'function_handle'.
PS: please don't change the formula, because this is just an example. My real problem is far more complicated than this. It has log and exp of this sum (integral(log(sum), -inf, inf)). So I can't break them up to do the integral individually and sum the integrals.I need to use sum(cellfun). Thank you.
Version: Matlab R2012a
Can anyone help me? Really appreciate.
You cannot add function handles, so anything that tries f{1}+f{2}+... would give an error.
But you can compute the integral of the sums like this, evaluating the function values one at a time and adding up the results:
function cellsum
f{1} = #(x) x;
f{2} = #(x) x.^2;
integral(#(x)addfcn(f,x), -3, 3)
end
function s = addfcn(f,x)
s = zeros(size(x));
for k = 1:length(f)
s = s + f{k}(x);
end
end
Note that x will usually be a vector when the integral command calls your functions with it. So your function definitions should be vectorized, .i.e., x.^2 instead of x^2, etc.

Numerically integrate in Matlab without requiring vector input

The following is a generalisation of my problem
function E = FunctionIntegration(S)
I = #(f) log(det(4 * S(f)));
E = integral(I, -pi, pi)
S is a function handle that takes scalar input f and returns a matrix. When I try and run this function I get a Inner matrix dimensions must agree error.
I understand that integral requires the function I to take vector input and that's where the problem lies but in this case I don't see a way of accommodating that as I must then pass this vector to function S which returns a matrix. Is there a way around this?
Note an example of S could be:
S = #(f) [f 0; 0 1]
Obviously in this case the integral is easy to do analytically but the function S can be any scalar to matrix transformation.
Your problem is that integral passes an array of values to I. But your I only expects a scalar. Try this:
function E = functionIntegration(S)
I = #(x) arrayfun(#(f) log(det(4 * S(f))), x);
E = integral(I, -pi, pi);
end
I've wrapped your integrand into a call to arrayfun which will loop over the array passed in by integral and calculates the integrand for each entry:
>> S = #(x) x * eye(3, 3);
>> functionIntegration(S)
ans =
28.8591 + 9.8696i