MATLAB - xy曲线下的条纹区域(弯矩分布)
MATLAB - striped area under the xy curve (bending moment distribution)
我想要实现的是一个经典的弯矩分布图,可能看起来像这样:
我尝试使用面积、xy 和条形图,最后一个最接近我需要的 - 但它仍然不是我可以接受的。我可以使用任意形式的数据。
我会手动解决它生成你想要的行。
%some example plot
x1 = -3;
x2 = 2;
upfun = @(x) -1/10*(x-x1).*(x-x2);
downfun = @(x) 1/5*(x-x1).*(x-x2);
%set slope you want. Inf for vertical lines
slope=inf;
x_dense = linspace(x1,x2,100);
x_sparse = linspace(x1,x2,20);
%plotting it without the stripes. nan is used not to have unintended lines connecting first and second function
plot([x_dense ,nan,x_dense ],[upfun(x_dense),nan,downfun(x_dense)])
x_stripes=nan(size(x_sparse).*[3,1]);
y_stripes=nan(size(x_sparse).*[3,1]);
if slope==inf
%vertical lines, no math needed to know the x-value.
x_stripes(1,:)=x_sparse;
x_stripes(2,:)=x_sparse;
else
%intersect both functions with the sloped stripes to know where they
%end
for stripe=1:numel(x_sparse)
x_ax=x_sparse(stripe);
x_stripes(1,stripe)=fzero(@(x)(upfun(x)-slope*(x-x_ax)),x_ax);
x_stripes(2,stripe)=fzero(@(x)(downfun(x)-slope*(x-x_ax)),x_ax);
end
end
y_stripes(1,:)=upfun(x_stripes(1,:));
y_stripes(2,:)=downfun(x_stripes(2,:));
x_stripes=reshape(x_stripes,1,[]);
y_stripes=reshape(y_stripes,1,[]);
plot([x_dense ,nan,x_dense,nan,x_stripes],[upfun(x_dense),nan,downfun(x_dense),nan,y_stripes])
坡度示例=1
斜率示例=inf
虽然 更通用,可用于倾斜条纹,但这里有一个更简单的解决方案,使用 stem
,没有标记和基线:
x1 = -3;
x2 = 2;
upfun = @(x) -1/10*(x-x1).*(x-x2);
downfun = @(x) 1/5*(x-x1).*(x-x2);
x_dense = linspace(x1,x2,100);
x_sparse = linspace(x1,x2,20);
%// plot outline
plot(x_dense,upfun(x_dense),'b-',x_dense,downfun(x_dense),'b-');
hold on;
%// plot stripes
stem(x_sparse,upfun(x_sparse),'b','marker','none','showbaseline','off');
stem(x_sparse,downfun(x_sparse),'b','marker','none','showbaseline','off');
结果:
我想要实现的是一个经典的弯矩分布图,可能看起来像这样:
我尝试使用面积、xy 和条形图,最后一个最接近我需要的 - 但它仍然不是我可以接受的。我可以使用任意形式的数据。
我会手动解决它生成你想要的行。
%some example plot
x1 = -3;
x2 = 2;
upfun = @(x) -1/10*(x-x1).*(x-x2);
downfun = @(x) 1/5*(x-x1).*(x-x2);
%set slope you want. Inf for vertical lines
slope=inf;
x_dense = linspace(x1,x2,100);
x_sparse = linspace(x1,x2,20);
%plotting it without the stripes. nan is used not to have unintended lines connecting first and second function
plot([x_dense ,nan,x_dense ],[upfun(x_dense),nan,downfun(x_dense)])
x_stripes=nan(size(x_sparse).*[3,1]);
y_stripes=nan(size(x_sparse).*[3,1]);
if slope==inf
%vertical lines, no math needed to know the x-value.
x_stripes(1,:)=x_sparse;
x_stripes(2,:)=x_sparse;
else
%intersect both functions with the sloped stripes to know where they
%end
for stripe=1:numel(x_sparse)
x_ax=x_sparse(stripe);
x_stripes(1,stripe)=fzero(@(x)(upfun(x)-slope*(x-x_ax)),x_ax);
x_stripes(2,stripe)=fzero(@(x)(downfun(x)-slope*(x-x_ax)),x_ax);
end
end
y_stripes(1,:)=upfun(x_stripes(1,:));
y_stripes(2,:)=downfun(x_stripes(2,:));
x_stripes=reshape(x_stripes,1,[]);
y_stripes=reshape(y_stripes,1,[]);
plot([x_dense ,nan,x_dense,nan,x_stripes],[upfun(x_dense),nan,downfun(x_dense),nan,y_stripes])
坡度示例=1
斜率示例=inf
虽然 stem
,没有标记和基线:
x1 = -3;
x2 = 2;
upfun = @(x) -1/10*(x-x1).*(x-x2);
downfun = @(x) 1/5*(x-x1).*(x-x2);
x_dense = linspace(x1,x2,100);
x_sparse = linspace(x1,x2,20);
%// plot outline
plot(x_dense,upfun(x_dense),'b-',x_dense,downfun(x_dense),'b-');
hold on;
%// plot stripes
stem(x_sparse,upfun(x_sparse),'b','marker','none','showbaseline','off');
stem(x_sparse,downfun(x_sparse),'b','marker','none','showbaseline','off');
结果: