MATLAB - striped area under the xy curve (bending moment distribution) [closed]

时光毁灭记忆、已成空白 提交于 2019-12-01 20:52:28

问题


what I want to achieve is a classical bending moment distribution plot that may look something like that:

I tried to use area, xy, and bar plot and the last one is the closest to what I need - but it's still not what I can accept. I can use data in arbitrary form.


回答1:


While Daniel's answer is more general, and can be used for slanted stripes, here's a simpler solution using stem without markers and baseline:

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');

Result:




回答2:


I would solve it manually generating the lines you want.

%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])

Example for slope=1

Example for slope=inf



来源:https://stackoverflow.com/questions/35965122/matlab-striped-area-under-the-xy-curve-bending-moment-distribution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!