how to fill color in 1/4 th circle

clear
syms x
figure
fun = sqrt(2*x-x.^2);
fplot(fun,[0,2.2]);
hold on
y1 = [0 1 0 0];
x1 = [0 1 1 0];
fill(x1,y1,'y');
hold on
x2 = 1:0.01:2;
y2 = sqrt(2*x2-x2.^2);
fill([x2,fliplr(x2)],[y2,fliplr(y2)],'r')

Risposte (1)

Star Strider
Star Strider il 15 Giu 2024
Modificato: Star Strider il 15 Giu 2024
A new function in R2024a is the polarregion function.
Try this —
figure
thetas1 = [0.5 1]*pi;
radii1 = [1 2];
polarregion(thetas1, radii1, 'FaceColor','y')
hold on
thetas2 = [0 0.5]*pi;
radii2 = [1 2];
polarregion(thetas2, radii2, 'FaceColor','r')
hold off
Ax = gca;
Ax.ThetaLim = [0 180];
Ax.RLim = [1 2];
Also see PolarAxes Properties for those details.
Doing it without using any of the polar functions is also straightforward, although easier using trigonometric functions,
Try this —
r1 = 1;
th1 = linspace(0, 90);
xy1 = [1+r1*cosd(th1); 1+r1*sind(th1)];
r2 = 1;
th2 = linspace(90, 180);
xy2 = [1+r2*cosd(th2); 1+r2*sind(th2)];
figure
patch([1 xy1(1,:)], [1 xy1(2,:)], 'r', 'EdgeColor','none' )
hold on
patch([1 xy2(1,:)], [1 xy2(2,:)], 'y', 'EdgeColor','none')
hold off
axis('equal')
Ax = gca;
Ax.Visible = 0;
Make appropriate changes to get the result you want.
.
EDIT — (15 Jun 2024 at 14:38)
Added second plot using the patch function.
.

10 Commenti

great, thank you very much
any universal way to fill the color in all kinds of area?
we have many funs like :fill, area, patch,..., hope just need one universal way
My pleasure!
If my Answer helped you solve your problem, please Accept it!
The patch function is essentially ‘universal’. To use it, it is necessary to create a closed contour, although the beginning and ending points do not need to be repeated, since the function connects them itself.
More examples of it are —
t = linspace(0, 2*pi, 500);
figure
patch(2*cos(3*t), 3*sin(2*t), 'y')
axis('equal')
figure
patch([3*t flip(3*t)], [2+cos(3*t) flip(sin(3*t))], 'm', 'FaceAlpha',0.5)
figure
patch([t flip(-t)], [exp(-t) -flip(exp(-t))], 'c')
figure
y = cos(t);
Lv = y >= -0.5; % Logical Index Vector
patch([t(Lv) flip(t(Lv))], [y(Lv) ones(size(t(Lv)))*2], 'r')
figure
y = sin(t);
Lv = y >= 0; % Logical Index Vector
patch([t(Lv) flip(t(Lv))], [sin(t(Lv)) zeros(size(t(Lv)))], 'g', 'DisplayName','Positive')
hold on
patch([t(~Lv) flip(t(~Lv))], [sin(t(~Lv)) zeros(size(t(~Lv)))], 'r', 'DisplayName','Negative')
hold off
legend('Location','best')
.
There are all sorts of things you can do with it, providing that you always express its arguments as closed contours.
.
thank you, very helpful
when I run
figure
y = cos(t);
Lv = y >= -0.5; % Logical Index Vector
patch([t(Lv) flip(t(Lv))], [y(Lv) ones(size(t(Lv)))*2], 'r')
I got:error using symengine......
first one is good, rest of them all got same error
Just trun them as I wrote them. Do not use the Symbolic Matrh Toolbox with them, since it is not necessary here.
If you have symbolic problems you need to solve and that use the patch function, please provide more details.
EDIT — (16 Jun 2024 at 14:17)
Added ‘Example’.
Note my changes to your original code.
Example —
syms x
figure
fun = sqrt(2*x-x.^2);
hfp = fplot(fun,[0,2.2], 'ShowPoles','off');
axis('equal')
x1 = hfp.XData;
y1 = hfp.YData;
hold on
% y1 = [0 1 0 0];
% x1 = [0 1 1 0];
fill(x1,y1,'y');
% hold on
Lv = x1 >= 1; % Logical Index Vector
% x2 = 1:0.01:2;
x2 = x1(Lv);
y2 = sqrt(2*x2-x2.^2);
fill([x2,fliplr(x2)],[zeros(size(y2)),fliplr(y2)],'r')
hold off
Doing this outside the Symbolic Math Toolbox is still easier. If you are deriving functions from symbolic calculations and derivations, use the matlabFunction function to create them as anonymous functions that you can use in numeric (rather than symbolic) calculations, and then call the functions with the appropriate arguments in your numeric code.
Be sure first to clear the symbolic variables before using the anonymous functions after you create all of them, so there will be no confusion regarding them in your numeric code.
.
thank you so much.
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Accedi per commentare.

Categorie

Prodotti

Release

R2024a

Tag

Richiesto:

il 15 Giu 2024

Commentato:

il 16 Giu 2024

Community Treasure Hunt

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

Start Hunting!

Translated by