What is the function to use to blacken a region?

1 view (last 30 days)
Lê
on 25 Nov 2023
Moved: Dyuman Joshi on 28 Nov 2023
I have got into MATLAB just for a week or two, so I am having trouble with functions. Can anyone tell me what function I should use to darken the region bounded by a graph and x-axis?
  3 Comments
Lê
on 25 Nov 2023
Moved: Dyuman Joshi on 28 Nov 2023
clear
%% Plotting the graphs
% We have p(x) = 16 => p(x) - 16 = 0
f = @(x) (800000 .* exp(-x./5000)) ./ (x+20000) - 16;
fimplicit(f), grid on
xlabel('x'); ylabel('p(x)');
I tried to graph this using fimplicit but all I get is a blank screen, any idea why and what should I fix?
Dyuman Joshi
Dyuman Joshi on 25 Nov 2023
Moved: Dyuman Joshi on 28 Nov 2023
fimplicit() is used for a different purpose. You want to use fplot here -
%% Plotting the graphs
% We have p(x) = 16 => p(x) - 16 = 0
f = @(x) (800000 .* exp(-x./5000)) ./ (x+20000) - 16;
fplot(f, [-1 1]*5e3), grid on
xlabel('x'); ylabel('p(x)');

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 25 Nov 2023
Perhaps this —
% clear
%% Plotting the graphs
% We have p(x) = 16 => p(x) - 16 = 0
f = @(x) (800000 .* exp(-x./5000)) ./ (x+20000);
xint = fzero(@(x)f(x)-16, 1)
xint = 3.7270e+03
x = linspace(0, 4000);
figure
plot(x, f(x), '-k', 'LineWidth',3)
hold on
patch([x flip(x)], [zeros(size(x))+16 flip(f(x))], [1 1 1]*0.8)
hold off
grid on
yline(16, '-k', 'LineWidth',2)
xlabel('x'); ylabel('p(x)');
text(xint, 16, sprintf('$(\\approx %.0f,16)$', xint), 'Interpreter','latex', 'Horiz','right', 'Vert','top', 'FontSize',14, 'FontWeight','bold')
pos = gca().Position;
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
annotation('arrow', xapf([1500 1000],pos,xlim), yapf([35 25],pos,ylim))
text(1500, 35, 'consumer surplus', 'Horiz','left', 'Vert','bottom', 'FontName','TimesRoman', 'FontSize',14)
Make appropriate changes to get the result you want.
.
  4 Comments
Lê
on 25 Nov 2023
Also, if I want to find the consumer surplus, just the function integral will do? By choosing x min = 0, and x max = xint.
Star Strider
Star Strider on 25 Nov 2023
Yes.
If you want the entire area from 0 to 4000, use those as the limits, otherwise use ‘xint’ as the upper limit —
f = @(x) (800000 .* exp(-x./5000)) ./ (x+20000);
format longG
xint = fzero(@(x)f(x)-16, 1)
xint =
3727.03867592715
Consumer_Surplus = integral(@(x)f(x)-16, 0, xint)
Consumer_Surplus =
37753.0101449318
Entire_Area = integral(@(x)f(x)-16, 0, 4000)
Entire_Area =
37611.8951504993
My apolgies for the delay — away doing other things for a few minutes.
.

Sign in to comment.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!