Generate inner values of y for each x based on n
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Saim Ehtesham
el 25 de Nov. de 2022
Comentada: Saim
el 25 de Nov. de 2022
I have the following code:
clear all
clc
n = 4; % number of divisions on the boundary
x1 = linspace(0,2,n);
y1_x1 = sqrt(1-( (x1.^2)/4 ) );
y2_x1 = -sqrt(1-( (x1.^2)/4 ) );
x2 = linspace(-2,0,n);
y1_x2 = sqrt(1-( (x2.^2)/4 ) );
y2_x2 = -sqrt(1-( (x2.^2)/4 ) );
x = [x1 x1 x2 x2];
y = [y1_x1 y2_x1 y1_x2 y2_x2];
scatter(x,y)
Now, is there a way to generate the inner values of y with respect to x?
For example, as we can see in the plot, an ellipse is formed, but the points are on the boundaries, I want points within the ellipse as well, such that it uses the values of x and y already calculated and connects the y values to x like at x = 0, y = 1.0000,0.9428,0.7454,0 and at next x on both positive and negative side (x = +-0.6667), y = 0.9428,0.7454,0 so on and so forth till x = +-2
But most importantly, it all stays connected, so that all I change is the value of n and the inner points are generated.
Thanks
0 comentarios
Respuesta aceptada
KSSV
el 25 de Nov. de 2022
clc; clear all ;
clear all
clc
n = 4; % number of divisions on the boundary
x1 = linspace(0,2,n);
y1_x1 = sqrt(1-( (x1.^2)/4 ) );
y2_x1 = -sqrt(1-( (x1.^2)/4 ) );
x2 = linspace(-2,0,n);
y1_x2 = sqrt(1-( (x2.^2)/4 ) );
y2_x2 = -sqrt(1-( (x2.^2)/4 ) );
x = [x1 x1 x2 x2];
y = [y1_x1 y2_x1 y1_x2 y2_x2];
scatter(x,y)
% Arrange ellipe points properly
xCenter = mean(x);
yCenter = mean(y);
angles = atan2d(y - yCenter, x - xCenter);
[sortedAngles, sortOrder] = sort(angles);
x = x(sortOrder);
y = y(sortOrder);
[X,Y] = meshgrid(linspace(min(x),max(x)),linspace(min(y),max(y))) ;
Z = zeros(size(X)) ;
idx = inpolygon(X,Y,x,y) ;
Z(idx) = 1 ;
pcolor(X,Y,Z)
Más respuestas (0)
Ver también
Categorías
Más información sobre Scatter Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



