30 views (last 30 days)
TT on 13 Oct 2014
Commented: Mike Hosea on 13 Oct 2014
Hello all,
Does anyone know how to integrate (x2*y3-x3*y2)+(y2-y3)*x+(x3-x2)*y; in MATLAB from domain x2 to x3 in X range and y2 to y3 in Y range?

Accepted Answer

Mike Hosea
Mike Hosea on 13 Oct 2014
Edited: Mike Hosea on 13 Oct 2014
First define a function. This function must be able to accept an array and return an array. That is to say, if y1 = f(x1) and y2 = f(x2), where x1 and x2 are scalars, the function must also return [y1,y2] for f([x1,x2]). Actually the inputs will tend to be dozens or hundreds of elements long. There is more than one way to define a function in MATLAB, but the quickest is with the @ symbol. When you write @(x,y) it means that what follows that will be a function of x and y. That is to say, x and y will be treated as names of the variables of the function, and MATLAB will not try to evaluate the expression right then and there. So, it looks to me like you want to
1. First define x2, x3, y2, and y3. These will be scalar values. For example,
x2 = 0;
x3 = 1;
y2 = 0;
y3 = pi;
2. Define
f = @(x,y) (x2*y3-x3*y2) + (y2-y3)*x + (x3-x2)*y
Normally, I would have told you to be sure to use .* instead of * because .* is element-by-element multiplication and * is matrix multiplication, but in this case you basically have an expression of the form a + b*x + c*y, where a, b, and c are scalars. When one argument is a scalar * and .* are the same operation. Note that if you want to change x2, x3, y2, or y3, you must define f again because when you create the function, it takes a snapshot of all the variables other than the arguments.
3. Perform the integral
Mike Hosea
Mike Hosea on 13 Oct 2014
OK. So what you're saying is that, just as we wanted to define the integrand with "arguments", we want to define our integral with arguments. No problem! But because the x2, x3, y2, and y3 variables with have their values saved when the integrand function is defined, we will need to bring the definition of the integrand in-line. It will look cluttered, but try to see it as one thing at a time from the inside out.
q = @(x2,x3,y2,y3)integral2(@(x,y)(x2*y3-x3*y2) + (y3-y2)*x + (x3-x2)*y,x2,x3,y2,y3);
When you're ready to supply the x2, y3, y2, and y3 values, you can do this:
>> q(0,1,0,pi)
ans =
I took the liberty of changing your problem slightly. If I had left the second term as (y2 - y3)*x, the correct answer would always have been zero.
Now, if you were wanting a symbolic integral, producing an expression involving x2, x3, y2, and y3, then that involves the int command. I'll do it with your original problem first:
>> int((x2*y3-x3*y2)+(y2-y3)*x+(x3-x2)*y,y,y2,y3)
ans =
((y2 - y3)^2*(x2 - 2*x + x3))/2
>> int(ans,x,x2,x3)
ans =
And on the slightly different problem:
>> int((x2*y3-x3*y2)+(y3-y2)*x+(x3-x2)*y,y,y2,y3)
ans =
((y2 - y3)^2*(2*x + x2 + x3))/2
>> int(ans,x,x2,x3)
ans =
-(x2^2 - x3^2)*(y2 - y3)^2

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!