Reputation: 101
I have a probability density function,
f(y) = a0*L_0 + a_neg*exp(A_neg *(y))*L_neg + a_pos*exp(-A_pos *(2-y))*L_pos, for 0<y<2.
All elements are available,
a0
is scalar,
L_0
is 1X5
,
a_neg
is 1X3
,
A_neg 3X3
,
L_neg
is 3X5
,
L_pos
1X5
,
a_pos
is scalar,
A_pos
is scalar.
Calculating it in matlab I get the result with no error,
% Define the function
f = @(y) a0*L_0 + a_neg*expm(A_neg * y)*L_neg + a_pos*exp(-A_pos * (2-y))*L_pos;
% Define the range of y
y = linspace(0, 2, 1000); % 1000 points between 0 and 2
% Initialize result matrix
result = zeros(length(y), 5);
% Compute the function for all y values
for i = 1:length(y)
result(i, :) = f(y(i));
end
% Print the result
disp(result);
This works properly, but since its a pdf function I want to take the integral and get a 1X5
vector as pdf integral from 1
to 2
f(y)dy
with this code,
f_integral = integral(@(y) sum(f(y)), 0, 2);
I get error "Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'." if there is a problem in my dimensions why I dont get error when I calculate f(y)
.
Upvotes: 0
Views: 62
Reputation: 101
this helped
f_values = zeros(200, 5); % Preallocate the matrix for f(y) values
for i = 1:200
f_values(i, :) = f(y_values(i));
end
integral_value = trapz(y_values, f_values);
Upvotes: 1
Reputation: 106
You can set the ArrayValued
option to true
as follows,
f_integral = integral(@(y) sum(f(y)), 0, 2, 'ArrayValued', true);
The problem is when you provide the function with a set of scalars like [1,2,3]
, it doesn't operate on every one of them individually. This is similar to what happens if the output itself is not a scalar but an array. That's why specifying that the output is an array even if it's actually not helps in this case.
Upvotes: 2