Matlab operation on anonymous functions - matlab

I have two anonymous functions f and g and I need to compute the integral over f*g using the quad function.
I tried the following which didnt not work:
h=#(x)(f(x)*g(x))
quad(h,-1,1);
Is there any way to make this work ?

Short answer
It depends on how f and g are defined, but you probably just need to add a dot:
h = #(x) f(x).*g(x);
Long answer
According to quad's documentation,
Q = quad(FUN,A,B)
[...] 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.
Assuming f and g already satisfy this requirement, h should be defined with .* (element-wise multiplication) so that it will fulfill it too.
Example:
>> f = #(x) x;
>> g = #(x) x.^2;
>> h = #(x) f(x).*g(x);
>> quad(h, 0, 1)
ans =
0.250000000000000

Related

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

Computing integral with variable bounds in MATLAB

Consider the following MWE in MATLAB:
f = #(t) integral(#(x) x.^2,0,t);
integral(f,0,1);
This yields the error
Error using integral (line 85) A and B must be floating-point scalars.
(and a bit more). How do I fix this? Is this even possible? I think the problem is the variable upper bound.
If you want to use integral then set 'ArrayValued' to true otherwise t would be an invalid end point in integral(#(x) x.^2,0,t). So it would be:
f = #(t) integral(#(x) x.^2,0,t);
integral(f,0,1,'ArrayValued',true)
% ans =
% 0.0833
Alternately, since you're doing double integration, so use the function dedicated for this purpose i.e. integral2. For your example, it would be:
f = #(t,x) x.^2 ;
integral2(f,0,1,0, #(t) t)
% ans =
% 0.0833
If you have Symbolic Math Toolbox, you can also use int as int(expr,var,a,b) but it would be slower. For your case, it would be:
syms x t;
f = x.^2;
req = int(int(f,x,0,t),t,0,1); % It gives 1/12
req = double(req); % Convert to double if required

differentiate a function in matlab

how can I differentiate a function in matlab? I tried with:
syms x;
f = 3x^2 + 2;
A = diff(f);
disp(A);
My problem is now I want to give x a value after (for example A(x = 1) and I don't know how.
First, there's an error in your code, you can't imply multiplication, you need to write 3*x^2 rather than 3x^2.
Also, in case you feed the function a vector rather than scalar, you should use element-wise operations (including a . before powers, multiplication etc.) so this becomes 3*x.^2.
Other than that, 2 options:
syms x;
f = 3*x.^2 + 2;
1) define A like you did and use subs to substitute x for, say, 1.
A = diff(f);
subs(A,x,1);
2) define A as an anonymous function and call it more easily.
A = #(y) subs(diff(f),x,y)
Now A can be easily called with different values
A(1)
ans = 6
A(2)
ans = 12
Links to documentation on element-wise ops, subs and anonymous functions:
https://uk.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
https://uk.mathworks.com/help/symbolic/subs.html
https://uk.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
Modify your code like this:
syms x;
f = 3*x^2 + 2; % You missed a * here
A(x) = diff(f); % This creates a symfun
A(1) % This gives the value of A at x=1
You need the matlabfunction function.
matlabFunction Generate a MATLAB file or anonymous function from a sym

Errors when using the Integral2 function in MATLAB

As far as I can tell, no one has asked this.
I've been asked to compute the double integral of a function, and also the same double integral but with the order of integration swapped (i.e: first integrate for dydx, then dxdy). Here is my code:
%Define function to be integrated
f = #(x,y) y^2*cos(x);
%First case. Integration order: dydx
ymin = #(x) cos(x);
I = integral2(f,ymin,1,0,2*pi)
%Second case. Integration order: dxdy
xmin = #(y) asin(y)+2*pi/2;
xmax = #(y) asin(y)-pi/2;
B = integral2(f,xmin,xmax,-1,1)
The error I'm getting is this:
Error using integral2 (line 71)
XMIN must be a floating point scalar.
Error in EngMathsA1Q1c (line 5)
I = integral2(f,ymin,1,0,2*pi)
I'm sure my mistake is something simple, but I've never used Integral2 before and I'm lost for answers. Thank you.
Per the integral2 documentation, the variable limits are given as the second pair of limits. So your first integral should be
% Define function to be integrated
f = #(x,y) y.^2.*cos(x);
% First case. Integration order: dydx
ymin = #(x) cos(x);
I = integral2(f,0,2*pi,ymin,1);
The set of constant limits always goes first, and Matlab assumes the first argument of f is associated with the first set of limits while the second argument of f is associated with the second set of limits, which may be a function of the first argument.
I point out that second part because if you wish to switch the order of integration, you also need to switch the order of the inputs of f accordingly. Consider the following example:
fun = #(x,y) 1./( sqrt(2*x + y) .* (1 + 2*x + y).^2 )
A nice little function that is not symmetric in its arguments (i.e., fun(x,y) ~= fun(y,x)). Let's integrate this over an elongated triangle in the first quadrant with vertices at (0,0), (2,0), and (0,1). Then integrating with dA == dy dx, we have
>> format('long');
>> ymax = #(x) 1 - x/2;
>> q = integral2(fun,0,2,0,ymax)
q =
0.220241017339352
Cool. Now let's integrate with dA == dx dy:
>> xmax = #(y) 2*(1-y);
>> q = integral2(fun,0,1,0,xmax)
q =
0.241956050772765
Oops, that's not equal to the first calculation! That's because fun is defined with x as the first argument and y as the second, but the previous call to integral2 is implying that y is the first argument to fun, and it has constant limits of 0 and 1. How do we fix this? Simply define a new function that flips the arguments:
>> fun2 = #(y,x) fun(x,y);
>> q = integral2(fun2,0,1,0,xmax)
q =
0.220241017706984
And all's right with the world. (Although you may notice small differences between the two correct answers due to the error tolerances of integral2, which can be adjusted via options per the documentation.)
The error states that you can't pass in a function for the limits of integration. You need to specify a scalar value for each limit of integration. Also, there are some errors in the dimensions/operations of the function. Try this:
%Define function to be integrated
f = #(x,y) y.^2.*cos(x);%changed to .^ and .*
%First case. Integration order: dydx
%ymin = #(x) cos(x);
I = integral2(f,-1,1,0,2*pi)%use scalar values for limits of integration
%Second case. Integration order: dxdy
%xmin = #(y) asin(y)+2*pi/2;
%xmax = #(y) asin(y)-pi/2;
B = integral2(f,0,2*pi,-1,1)% same issue, must use scalars

Functions in matlab

This looks really simple. I want to define a function:
syms x
f = x^2
I want to be able to do f(4) and it spits out 16. I also want to avoid having to write a new m-file.
When dealing with symbolic variables, to substitute in a numeric value, use subs(), i.e. symbolic substitution:
syms x
f = x^2
subs(f,4)
>> f = #(x) x^2;
>> f(4)
ans =
16