What is the function to use to blacken a region?
1 view (last 30 days)
Show older comments
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
Dyuman Joshi
on 25 Nov 2023
Moved: Dyuman Joshi
on 28 Nov 2023
%% 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)');
Answers (1)
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)
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
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)
Consumer_Surplus = integral(@(x)f(x)-16, 0, xint)
Entire_Area = integral(@(x)f(x)-16, 0, 4000)
My apolgies for the delay — away doing other things for a few minutes.
.
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!