Numerical integral with syms variables

42 views (last 30 days)
Hossein
Hossein on 23 Sep 2019
Commented: Biyi Wang on 31 Jan 2024
Hi everybody,
I am trying to make numerical integral from a function which consists of several symbolic. It's clear that I want to make the integral with respect to a single symbolic variable (x). Can anybody help me doing this task?
many thanks in advance.
syms A B C x
f=@(x) A*x^2+B*x+C
integral(@(x) f, -1, 2)

Answers (1)

Steven Lord
Steven Lord on 23 Sep 2019
If you had numeric values for A, B, and C then integral would be the right tool for the job. Note that nowhere in this code do I create a symbolic variable.
A = 1;
B = 2;
C = 3;
f = @(x) A*x.^2+B*x+C; % Added the period to vectorize f
result1 = integral(f, -1, 2)
I suspect you want the expression of the result to contain symbolic variables A, B, and C. If so, use int instead of integral. For this, I do need to create symbolic variables.
syms A B C x
f = A*x^2+B*x+C;
result2 = int(f, x, -1, 2)
We can confirm that result2 gives the same value as result1 when we substitute the values from the first section of code into the symbolic answer.
check = subs(result2, [A B C], [1 2 3])
  6 Comments
Walter Roberson
Walter Roberson on 29 Jan 2024
The number of terms in the integration goes up as roughly (2 times size) quantity squared, and most of those terms involve division by t. The integration involves comparing each of the terms to each of the other terms, trying to figure out whether each can be used to complete the division derivative.
Biyi Wang
Biyi Wang on 31 Jan 2024
Thanks for your nice reply!
Could you please make it more clear about this following comment?
"The integration involves comparing each of the terms to each of the other terms, trying to figure out whether each can be used to complete the division derivative."

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!