How to shade the area bounded by curves

8 ビュー (過去 30 日間)
Joemama22
Joemama22 2015 年 11 月 2 日
コメント済み: Star Strider 2022 年 11 月 20 日
I need to shade the area bounded by the curves y=x^2, the x-axis, and y= -(1/4)*x+(9/2) the color yellow. I can't figure out how to do this. I'm close, and I'm sure it's an easy fix, but here's what I have so far:
clc;clear;close all;
syms x;
par= int(x^2,0,2);
dy= diff(x^2);
lin= int(-(1/4)*x+(9/2),2,18);
y1=x^2;
y2=-(1/4)*x+(9/2);
area=(par+lin)
hold on
fplot('x^2',[0 2])
fplot('-(1/4)*x+(9/2)',[2 18])
%fill(x,y,'y')
hold off
title('Supplement 7: Problem 4')
axis equal; axis([0 20 0 5])
set(gca, 'Xtick', 0:20)
set(gca, 'Ytick', 0:5)
xlabel('x'); ylabel('y')

採用された回答

Star Strider
Star Strider 2015 年 11 月 2 日
A somewhat simpler approach:
x=linspace(0, 18);
y=x.^2;
y2=x.*(-1./4)+(9./2);
y3=x.*0;
figure(1)
plot(x,y, x,y2, x,y3); grid on;
axis([-1,20,-1,5])
area(x, min([y; y2]), 'FaceColor','y')
  2 件のコメント
Joshua
Joshua 2022 年 11 月 20 日
for some reason, the area() function isnt working
Star Strider
Star Strider 2022 年 11 月 20 日
@Joshua — I have no idea what ‘isn’t working’ means.
That consideration aside, patch is an option —
x=linspace(0, 18);
y=x.^2;
y2=x.*(-1./4)+(9./2);
y3=x.*0;
figure(1)
plot(x,y, x,y2, x,y3); grid on;
axis([-1,20,-1,5])
hold on
patch([x flip(x)], [min([y; y2]) zeros(size(x))], 'y', 'EdgeColor','none')
hold off
The code changes slightly with patch in order to create a closed region for it to fill.
.

サインインしてコメントする。

その他の回答 (1 件)

TastyPastry
TastyPastry 2015 年 11 月 2 日
This should do it. Modified from this.
t=(-10:0.01:10)'; %values on x axes
f=t.^2; %values on y axes, put your function here
mif=zeros(numel(t),1);
maf=-.25.*t+4.5;
%select minimum and maximum value (y axes)
cla
axis([-10 10 -10 10])
hold on
line(xlim,[mif mif],'LineStyle','Color','k')
line([t(1) t(end)],[maf(1) maf(end)],'LineStyle','--','Color','g')
idx=f>mif & f<maf; %select the index values of f that satisfy the conditions
plot(t,f) %plot the function
fill(t(idx),f(idx),'r') %fill the area that's inside the conditions
legend('maximum','minimum','function','area')
  1 件のコメント
Joemama22
Joemama22 2015 年 11 月 2 日
When I put this code into matlab, nothing happens.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by