Error in solving the integral series: Error using integral (line 85) A and B must be floating-point scalars."

1 view (last 30 days)
I need to evaluate the integrals defined in a series where the second integral term uses the output of previous integral as a function. The program is as below:
clc;
clear all;
q1 = @(x) integral(@(y) j1(x,y).*w(y),0,x); %q1=int(xy^2)=x^4/3
q1(1) %=1/3
q2 = @(x) integral(@(y) j1(x,y).*q1(y),0,x); %q2=int(xy^5/3)=x^7/18
q2(1) %=1/18
q3 = @(x) integral(@(y) j1(x,y).*q2(y),0,x); %q3=int(xy^8/18)=x^10/162
q4 = @(x) integral(@(y) j1(x,y).*q3(y),0,x);
result=q1(1)+q2(1)+q3(1)+q4(1);
function q0=j1(x,y)
q0=x.*y; % A simple test function
end
function q1=w(x)
q1 = x; % A simple test function
end
Matlab is able to evaluate q1(1) but q2 and next terms it's not able to evaluate and throws the error.
The finctions 'j1' and 'w' used here simplified for testing, however actual functions I aim to use are very large such that using symbolic method for integration takes hour and hours of run time.
How can I solve this error or is there any other moethod that can be used to solve this series using matlab?
Thanks and Regards,
Amit
  6 Comments
Amit
Amit on 10 Apr 2020
Ameer, using 'ArrayValued' solved the error. Thanks.
Also, evaluation of q4 took quite some time, as you said.
alternative 1: Symbolic method takes too long for my functions. Matlab was still busy after 7-8 hours so had to terminate forcefully.
alternative 2: integral2, integral3, etc. I am not able to write integral2 statement for q2. There doesn't seem to be any straight forward way.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!