Multiplication Error in Integral Function in MATLAB - matlab

I want to write a function which uses a combination of sin(x) and cos(x) functions and then integrate it to get a scalar value as a result. However, when I run the code I got an error which says that dimensions of the matrices do not match in the integral function but they actually match. I an multiplying a 1x2 matrix with a 2x1 matrix. I am supposed to get a scalar value for the multiplication. Can you help please?
y = zeros(2,2);
y(1,1) = 100;
y(1,2) = 5;
y(2,1) = 200;
y(2,2) = 10;
fun = #(x) ([sin(x) cos(x)] * [y(:,1) - y(:,2)]);
q = integral(fun,0,Inf);

I found a solution to this problem. Instead of using matrices, I converted the function to a scalar form. This way it doesn't give a multiplication error.
fun = #(x) sin(x)*(y(1,1)-y(1,2)) + cos(x)*(y(2,1)-y(2,2));

Related

Newton–Raphson method with vector inputs in MATLAB

For this problem, we need to make use of the Newton-Raphson method to locate the roots of a particular function.
The code works for when the input is a single value, yet when the input is a vector, the answers aren't quite right.
For example, when x=2 is an input, the value 2.5933 is returned and when x=4, 4.3215 is returned. Both these answers are correct, yet when I enter the vector x = [2,4], it returns [2.4106,4.4106].
f = #(x) [(17/77196).*x.^(3)-(15/12866).*x.^(2)+0.004];
fd = #(x) [(17/25732).*x.^(2)-(15/6433).*x];
x= %initial guess;
for i=1:10
x=x-f(x)/fd(x);
end
You can try this
f = #(x) [(17/77196).*x.^(3)-(15/12866).*x.^(2)+0.004];
fd = #(x) [(17/25732).*x.^(2)-(15/6433).*x];
x= [2, 4];
for i = 1:10
x = x - f(x)./fd(x);
end
x
You were missing . after f(x) to make it an element-wise division.

MatLab Quadratic Equation With ln

How to solve the function f(x)=ln(x^2)-0.7=0 with a known Matlab command?
clc;clear all;close all;
f(x)=ln(x^2)-0.7=0
B=sqrt f(x)
You can use symbolic variables together with the solve function:
syms x;
eqn = log(x^2) - 0.7 == 0;
solve(eqn,x)
The above code will output:
ans =
exp(7/20)
-exp(7/20)
Since the equation is quadratic, the solver returns two distinct solutions (often people forget that quadratic equations may have two specular solutions, one positive and one negative).
If you want to retrieve the numerical values (for example, in order to calculate their sqrt value):
sol = solve(eqn,x);
num = double(sol)
num =
1.4191
-1.4191
Put the following code into a MATLAB script, name it "main.m".
function b=main
clc
x=solveF()
y=f(x)
b=sqrt(y)
end
function y=f(x)
y=log(x^2)-0.7
end
function x=solveF()
g = #(x) abs(f(x)-0)
x = fminsearch(g, 1.0)
end
Then run it as:
main
You will get the results:
x =
1.4190
y =
-3.4643e-05
b =
0.0000 + 0.0059i
ans =
0.0000 + 0.0059i
You can define equations in matlab as such:
f = #(x) log(x^2)-0.7;
B = #(x) sqrt(f(x));
If you want to find the value of x satisfying a constraint you can design a function that will be equal to zero when the constraint is respecte, then call fminsearch to find x:
f_constraint = #(x) abs(f(x)-0);
x_opt = fminsearch(f_constraint, 1.3); % function handle, initial estimate
In your example, B(x_opt) should be equal to zero. This is not exactly the case as fminsearch estimated a solution.

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

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