I got the problem with polyshape, please help me

Demand: sketch the domain D be the region bounded by
My code:
x1 = 0:1:30;
y1 = 2-x1;
y2 = sqrt(x1).*(12-x1)./2;
xcut = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 0.5);
x = [x1(x1<=xcut), x1(end)];
y = [sqrt(x1).*(12-x1)./2 (x1<=xcut), 2-x1(end)];
M = polyshape(x,y);
plot(x1,y1,x1,y2,x,y)
hold on
plot(M)
hold off
xlim([0,30])
ylim([0,30])
Error:
Error using polyshape/getXY
x- and y-coordinates must be vectors of the same size with at least 3 elements.
Error in polyshape/checkInput (line 842)
[X, Y, xy2input, next_arg] = polyshape.getXY(varargin{:});
Error in polyshape (line 169)
[X, Y, tc, simpl, collinear] = polyshape.checkInput(param, varargin{:});
Error in bai4 (line 7)
M = polyshape(x,y);

4 个评论

The domain of the first function is x>=0 and the domain of y=2-x is all .Neither domain is bounded, and therefore cannot be sketched.
As well, you cannot use polyshape on unbounded regions.
oh I'm sorry.
The demand is sketch the domain D be the region bounded by

请先登录,再进行评论。

 采纳的回答

You have to take both points into account to find a closed region:
xcut1 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 0.5);
xcut2 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 20);
I had the code ready here - but homework is homework... I leave the rest to you. It should be easy now...

12 个评论

I appreciate that a lot :3
Thanks
sorry but I still cannot solve it
Is this right ?
x1 = 0:1:30;
y1 = 2-x1;
y2 = sqrt(x1).*(12-x1)./2;
xcut1 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 0.5);
xcut2 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 20);
x = [x1(x1>=xcut1), x1(x1<=xcut2)];
y = [sqrt(x1).*(12-x1)./2 -2+x1(x1>=xcut1),sqrt(x1).*(12-x1)./2 -2+x1(x1<=xcut2)];
M = polyshape(x,y);
plot(x1,y1,x1,y2,x,y)
hold on
plot(M)
hold off
xlim([0,30])
ylim([0,30])
can you explained it clearly or give me some hints please .
Thanks in advance @Stephan
your x used for the polyshape should look like:
x = xcut1:0.1:xcut2;
then think about the corresponding y values...
Hint: Use the false flag for the simplify option of the polyshape function to suppress warnings:
M = polyshape([YOUR X],[YOUR Y], 'simplify', false);
I tried but it still cannot work.
x1 = 0:1:30;
y1 = 2-x1;
y2 = sqrt(x1).*(12-x1)./2;
xcut1 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 0.5);
xcut2 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 20);
x = xcut1:0.1:xcut2;
ycut1 = fzero(@(x)sqrt(xcut1).*(12-xcut1)./2 - 2+xcut1, 0.5);
Exiting fzero: aborting search for an interval containing a sign change because no sign change is detected during search. Function may not have a root.
ycut2 = fzero(@(x)sqrt(xcut2).*(12-xcut2)./2 - 2+xcut2, 20);
Exiting fzero: aborting search for an interval containing a sign change because no sign change is detected during search. Function may not have a root.
M = polyshape([xcut1 ycut1],[xcut2 ycut2], 'simplify', false);
Warning: Boundaries with less than 3 points were removed.
plot(x1,y1,x1,y2,xcut1,xcut2,ycut1,ycut2)
hold on;
plot(M)
hold on;
grid on;
xlim([0,30])
ylim([0,30])
ycut1 and ycut2 dont make sense.
Think about how polyshape works:
  • you need a closed region --> so far we know closing points in xdirection are xcut1 and xcut2
  • inside this interval of x-values of your ROI you need correspondig y-values
  • you need several points for the nonlinear part of your region
  • the linear part of your region should be able to be described using 2 points --> you can guess which points this will be...
  • using this thoughts you can construct a vector pair x,y that describes the ROI properly for the usage of polyshape
I change it into this
x1 = 0:1:30;
y1 = 2-x1;
y2 = sqrt(x1).*(12-x1)./2;
xcut1 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 0.5);
xcut2 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 20);
x = xcut1:0.1:xcut2;
y = 2-x1(x1>=xcut1) :0.1: 2-x1(x1<=xcut2);
M = polyshape([xcut1 y],[xcut2 y], 'simplify', false);
plot(x1,y1,x1,y2,x,y)
hold on;
plot(M)
hold on;
grid on;
xlim([0,30])
ylim([0,30])
But it's still wrong in plot(x1,y1,x1,y2,x,y)
x1 = 0:1:30;
y1 = 2-x1;
y2 = sqrt(x1).*(12-x1)./2;
xcut1 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 0.5);
xcut2 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 20);
x = xcut1:0.1:xcut2;
y = 2-x1(x1>=xcut1) :0.1: 2-x1(x1<=xcut2);
M = polyshape([xcut1 y],[xcut2 y], 'simplify', false);
plot(x1,y1,x1,y2,x,y)
hold on;
plot(M)
hold on;
grid on;
xlim([0,30])
ylim([0,30])
it still doesn't work
inside this interval of x-values of your ROI you need corresponding y-values - that means to calculate them
rectangle example:
x = [1 5 5 1];
y = [1 1 2 2];
M = polyshape(x,y);
plot(M)
xlim([0 6])
ylim([0 6])
Try to find out how it works - then you get it
x1 = 0:1:30;
y1 = 2-x1;
y2 = sqrt(x1).*(12-x1)./2;
xcut1 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 0.5);
xcut2 = fzero(@(x)sqrt(x).*(12-x)./2 - 2+x, 20);
a=[x1(xcut1<=x1),x1(x1<=xcut2)];
b=[2-x1 (x1>=xcut1), 2-x1 (x1<=xcut2)] ;
M = polyshape(a,b);
plot(M)
hold on;
grid on;
xlim([0,30])
ylim([0,30])
..... I tried but I cannot mark the curve region
Sorry, last try - i can not make it more clear without doing your homework:
x = -10:1:10;
y1 = -x.^2 + 100;
y2 = 0.*x + 50;
xcut1 = fzero(@(x)-x.^2 + 100 - 50,-7);
xcut2 = fzero(@(x)-x.^2 + 100 - 50,7);
x_linear = [xcut1 xcut2];
y_linear = [50 50];
x_quad = linspace(xcut1, xcut2, 25);
y_quad = -x_quad.^2 + 100;
M = polyshape([x_linear, x_quad], [y_linear, y_quad],'simplify', false);
plot(x,y1,x,y2)
hold on
plot(M)

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by