MATLAB Answers

Evaluating the area between two curves.

176 views (last 30 days)
Ejay Kasai
Ejay Kasai on 12 Sep 2011
how to Evaluate the area between two curves using matlab? for example this one y=x^2 and y=2*x


Sign in to comment.

Answers (1)

Grzegorz Knor
Grzegorz Knor on 12 Sep 2011
In your case:
syms x
x0 = eval(solve('x^2 -2*x'));
x = linspace(x0(1),x0(2),100);
y1 = x.^2;
y2 = 2*x;
fill([x x(end:-1:1)],[y1 y2(end:-1:1)],'r')
hold on
grid on
a = trapz(x,y2)-trapz(x,y1);
Instead of:
syms x
x0 = eval(solve('x^2 -2*x'));
You can use:
x0(1) = fzero('x.^2 -2*x',1);
x0(2) = fzero('x.^2 -2*x',1.5);


Show 3 older comments
Oleg Komarov
Oleg Komarov on 12 Sep 2011
Grzegorz has given you a valid solution. If you really needed the value you wouldn't have posted the comment in the first place since it's in the code.
Ejay Kasai
Ejay Kasai on 13 Sep 2011
i mean i need a code to evaluate the area between the two curves. i need to make a matlab program for that.
Walter Roberson
Walter Roberson on 13 Sep 2011
The expression Grzegorz gave, a = trapz(x,y2)-trapz(x,y1) *is* the code to evaluate the area between the two curves. This is elementary calculus: the area between two curves is the difference between their integrals. trapz() calculates numeric integrals.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!