Search code examples
matlabmatrix-multiplicationintegral

Multiplication Error in Integral Function in 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);

Solution

  • 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));