File:Lever in balance hyperbola.gif

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Lever_in_balance_hyperbola.gif(500 × 450 pixels, file size: 2.94 MB, MIME type: image/gif, looped, 221 frames, 12 s)

Captions

Captions

Add a one-line explanation of what this file represents

Summary[edit]

Description
Deutsch: Hebel
Date
Source Own work
Author Jahobr
Other versions
GIF development
InfoField
 
This diagram was created with MATLAB by Jahobr.
Source code
InfoField

MATLAB code

function lever_in_balance()
% source code that produces a GIF and a SVG
%
% 2017-11-01 Jahobr


rampUpDown = [0:0.1:1 linspace(1,2,90) 2:-0.2:0]; % speed profile
rampUpDown = cumsum(rampUpDown); % distance
rampUpDown = rampUpDown(1:end-1); % remove redundant point
rampUpDown = rampUpDown./rampUpDown(end); % normalize (0 to 1)

xPosList = [(0.5+rampUpDown./2) (1+rampUpDown(2:end))]; % x position with intermediate stop
nFrames = numel(xPosList);
[pathstr,fname] = fileparts(which(mfilename)); % save files under the same name and at file location

leverThickness = 0.05;
leverLength = [-1.1 2.1];
leverOff = 0.17; % lever offset

xH = 0.3 : 0.01 : 2.3; % Hyperbola
yH = 1./xH;            % Hyperbola

%% create figure
figHandle = figure(15677755); clf
set(figHandle,'Units','pixel');
set(figHandle,'GraphicsSmoothing','on') % requires at least version 2014b
axesHandle = axes;
hold(axesHandle,'on')

versionList{1} = 'hyperbola'; % full version with force vectors and hyperbola
versionList{2} = 'weights'; % reduced; just lever and weights

for versionNr = 1:numel(versionList)
    cla(axesHandle) % fresh frame
    saveName = [fname '_' versionList{versionNr}];
    
    RGB = struct;
    RGB.black     = [0    0    0  ];
    RGB.white     = [1    1    1  ];
    RGB.leverEdge = [0.55 0.27 0.08]; % brown
    RGB.leverFace = [0.70 0.5  0.35]; % brown
    
    RGB.weight  = [0.6  0.6  0.6]; % grey
    RGB.weight2 = [0.7  0.7  0.7]; % grey shine
    RGB.weight3 = [0.8  0.8  0.8]; % grey glare
    
    RGB.coordinate = [0.3 0.3 0.3]; % grey
    if versionNr == 1
        xSize = 500; % size in pixel
        ySize = 450; % size in pixel
        scaleReduction = 2; % the size reduction: adds antialiasing
        xLimits = [-1.42  2.38];
        yLimits = [-0.98  2.25];
        RGB.LForce = [0 0 0.8]; % blue
        RGB.RForce = [1 0 0  ]; % red
        RGB.LText  = [0 0 0.3]; % blue
        RGB.RText  = [0.2 0 0]; % red
        
        RGB.LArea  = [0.7 0.7 1]; % blue
        RGB.RArea  = [1 0.7 0.7]; % red
        RGB.Hyper  = [1 0.7 0.7]; % red
    else
        xSize = 900; % size in pixel
        ySize = 250; % size in pixel
        scaleReduction = 2; % the size reduction: adds antialiasing
        xLimits = [-1.42  2.38];
        yLimits = [-0.95  0.1];
    end
    RGB = structfun(@(q)round(q*255)/255, RGB, 'UniformOutput',false); % round to values that are nicely uint8 compatible
    
    liSc = xSize*scaleReduction/1000;
    set(figHandle,'Position',[1 1 [xSize ySize]*scaleReduction]); % big start image for antialiasing later [x y width height]
    set(axesHandle,'Position',[-0.05 -0.05 1.1 1.1]); % stretch axis bigger as figure, easy way to get rid of ticks [x y width height]
    xlim(xLimits); ylim(yLimits); axis equal; drawnow;
    
    %% plot loop
    reducedRGBimage = uint8(ones(ySize,xSize,3,nFrames)); % allocate
    for iFrame = 1:nFrames
        cla(axesHandle) % fresh frame
        
        xPos = xPosList(iFrame);
        
        if versionNr == 1
            forceR = 1/xPos;
            weightL = 1000/9.81; %
            weightR = 1000*forceR/9.81; %
            plotBox([-1 0],[0 1],RGB.LArea,'none',1)
            plotBox([ 0 xPos],[0 1/xPos],RGB.RArea,'none',1)
        else
            weightL = 100;
            weightR = 100/xPos;
        end
        
        plot(leverLength+0.05,[0 0],'-','Color',RGB.coordinate,'LineWidth',2.5*liSc) % horizontal arrow
        plot(leverLength(2)+0.05+[-0.08  0    -0.08],[-0.02 0 0.02],'-','Color',RGB.coordinate,'LineWidth',2.5*liSc) % Arrowhead
        plot(leverLength(2)+0.05+[-0.08 -0.02 -0.08],[-0.02 0 0.02],'-','Color',RGB.coordinate,'LineWidth',2.5*liSc) % Arrowhead
        
        
        plotBox(leverLength,[-leverThickness 0]-leverOff,RGB.leverFace,RGB.leverEdge,2.5*liSc) % lever
        
        for scale = ceil(leverLength(1)) :0.1: floor(leverLength(2))
            if scale*2 == round(scale*2)
                plot([scale scale],[-0.007 -leverThickness+0.007]-leverOff,'Color',RGB.leverEdge,'LineWidth',5*liSc) % bar scale
                plot([scale scale],[-0.02 +0.02],'-','Color',RGB.coordinate,'LineWidth',5*liSc) % coordinate scale
                text(scale,-0.01, num2str(scale),...
                    'FontWeight','bold', 'FontName','Helvetica Narrow',...
                    'Color',RGB.coordinate, 'FontUnits','pixel', 'FontSize',33*liSc,...
                    'HorizontalAlignment','center', 'VerticalAlignment','top');
            else
                plot([scale scale],[-0.009 -leverThickness+0.009]-leverOff,'Color',RGB.leverEdge,'LineWidth',2.5*liSc) % bar scale
                plot([scale scale],[-0.015 +0.015],'-','Color',RGB.coordinate,'LineWidth',2.5*liSc) % coordinate scale
            end
        end
        
        if versionNr == 1
            text(2.03,0.03,'[m]',...
                'FontWeight','bold', 'FontName','Helvetica Narrow',...
                'Color',RGB.coordinate, 'FontUnits','pixel', 'FontSize',33*liSc,...
                'HorizontalAlignment','left', 'VerticalAlignment','bottom');
            
            plot(xH,yH,':','Color',[1 0.1 0.1],'LineWidth',7*liSc) % Hyperbola
            
            plot([0 0],[0 2.15],'-','Color',RGB.coordinate,'LineWidth',2.5*liSc) % vertical arrow
            plot([-0.02 0 0.02],[2.07 2.15 2.07],'-','Color',RGB.coordinate,'LineWidth',2.5*liSc) % Arrowhead
            plot([-0.02 0 0.02],[2.07 2.13 2.07],'-','Color',RGB.coordinate,'LineWidth',2.5*liSc) % Arrowhead
            
            for scale = 0:0.1:2.0
                if scale*2 == round(scale*2)
                    plot([-0.02 +0.02],[scale scale],'-','Color',RGB.coordinate,'LineWidth',5*liSc) % coordinate scale
                    if scale >0
                        text(-0.03,scale, num2str(scale,'%3.1f'),...
                            'FontWeight','bold', 'FontName','Helvetica Narrow',...
                            'Color',RGB.coordinate, 'FontUnits','pixel', 'FontSize',33*liSc,...
                            'HorizontalAlignment','right', 'VerticalAlignment','middle');
                    end
                else
                    plot([-0.015 +0.015],[scale scale],'-','Color',RGB.coordinate,'LineWidth',2.5*liSc) % coordinate scale
                end
            end
            text(-0.05,2.12, 'F [kN]',...
                'FontWeight','bold', 'FontName','Helvetica Narrow',...
                'Color',RGB.coordinate, 'FontUnits','pixel', 'FontSize',33*liSc,...
                'HorizontalAlignment','right', 'VerticalAlignment','middle');
            
            
            text(-0.5,1, '1.00 m',...
                'FontWeight','bold', 'FontName','Helvetica Narrow',...
                'Color',RGB.LText, 'FontUnits','pixel', 'FontSize',45*liSc,...
                'HorizontalAlignment','center', 'VerticalAlignment','cap');
            text(-0.98,0.1, '1.00 kN',...
                'FontWeight','bold', 'FontName','Helvetica Narrow',...
                'Color',RGB.LText, 'FontUnits','pixel', 'FontSize',45*liSc,...
                'HorizontalAlignment','left', 'VerticalAlignment','bottom');
            text(-0.5,0.46, '$${\bf M_L}$$',...
                'interpreter','Latex',...
                'Color',RGB.LText, 'FontUnits','pixel', 'FontSize',70*liSc,...
                'HorizontalAlignment','center', 'VerticalAlignment','middle');
            
            plot([-1 -1],[0.04 1],'-','Color',RGB.LForce,'LineWidth',5.0*liSc)
            plot([-0.02 0 0.02]-1,[0.09 0.01 0.09],'-','Color',RGB.LForce,'LineWidth',2.5*liSc) % Arrowhead
            plot([-0.02 0 0.02]-1,[0.09 0.04 0.09],'-','Color',RGB.LForce,'LineWidth',2.5*liSc) % Arrowhead
            
            
            text(xPos/2,forceR, [num2str(xPos,'%1.2f') ' m'],...
                'FontWeight','bold', 'FontName','Helvetica Narrow',...
                'Color',RGB.RText, 'FontUnits','pixel', 'FontSize',45*liSc,...
                'HorizontalAlignment','center', 'VerticalAlignment','cap');
            text(xPos-0.02,0.1, [num2str(forceR,'%1.2f') ' kN'],...
                'FontWeight','bold', 'FontName','Helvetica Narrow',...
                'Color',RGB.RText, 'FontUnits','pixel', 'FontSize',45*liSc,...
                'HorizontalAlignment','right', 'VerticalAlignment','bottom');
            text(xPos/2,forceR*0.46, '$${\bf M_R}$$',...
                'interpreter','Latex',...
                'Color',RGB.RText, 'FontUnits','pixel', 'FontSize',70*liSc,...
                'HorizontalAlignment','center', 'VerticalAlignment','middle');
            
            plot([xPos    xPos],     [0.04    forceR],'-','Color',RGB.RForce,'LineWidth',5.0*liSc)
            plot([-0.02 0 0.02]+xPos,[0.09 0.01 0.09],'-','Color',RGB.RForce,'LineWidth',2.5*liSc) % Arrowhead
            plot([-0.02 0 0.02]+xPos,[0.09 0.04 0.09],'-','Color',RGB.RForce,'LineWidth',2.5*liSc) % Arrowhead
            
            text(2.15,2, '$${\bf M_L+M_R =0}$$',...
                'interpreter','Latex',...
                'FontUnits','pixel', 'FontSize',70*liSc,...
                'HorizontalAlignment','right', 'VerticalAlignment','middle');
            text(0.95,1.7, '$${\bf M_L}$$',...
                'interpreter','Latex',...
                'FontUnits','pixel', 'FontSize',50*liSc,...
                'HorizontalAlignment','left', 'VerticalAlignment','middle');
            text(2.15,1.7, '$${\bf =+1\,kNm}$$',...
                'interpreter','Latex',...
                'FontUnits','pixel', 'FontSize',50*liSc,...
                'HorizontalAlignment','right', 'VerticalAlignment','middle');
            text(0.95,1.5, '$${\bf M_R}$$',...
                'interpreter','Latex',...
                'FontUnits','pixel', 'FontSize',50*liSc,...
                'HorizontalAlignment','left', 'VerticalAlignment','middle');
            text(2.15,1.5, '$${\bf =-1\,kNm}$$',...
                'interpreter','Latex',...
                'FontUnits','pixel', 'FontSize',50*liSc,...
                'HorizontalAlignment','right', 'VerticalAlignment','middle');
           
        end
        bearing(0,-leverThickness-leverOff,0.3)
        
        disc(0,-leverThickness-leverOff, 0.02,RGB.white,RGB.black,2.5*liSc)
        
        plotWeight(-1,  -leverOff,weightL)
        plotWeight(xPos,-leverOff,weightR)
        
        %% save animation
        xlim(xLimits); ylim(yLimits); drawnow; % set axis limits
        
        f = getframe(figHandle);
        reducedRGBimage(:,:,:,iFrame) = imReduceSize(f.cdata,scaleReduction); % the size reduction: adds antialiasing
        
        if any(xPos == [0.5 1 2]) % SVG
            disp(['x=' num2str(xPos) ' at Frame ' num2str(iFrame)]);
            if ~isempty(which('plot2svg'))
                plot2svg(fullfile(pathstr, [saveName '_1-' num2str(xPos) '.svg']),figHandle) % by Juerg Schwizer
            else
                disp('plot2svg.m not available; see http://www.zhinst.com/blogs/schwizer/');
            end
        end
        
    end
    
    startMap = cell2mat(struct2cell(RGB)); % struct2colormap; % list of map colors that are not allowed to be changed
    map = createImMap(reducedRGBimage,32,startMap); % colormap
    
    im = uint8(ones(ySize,xSize,1,nFrames)); % allocate
    for iFrame = 1:nFrames
        im(:,:,1,iFrame) = rgb2ind(reducedRGBimage(:,:,:,iFrame),map,'nodither');
    end
    
    imwrite(im,map,fullfile(pathstr, [saveName '.gif']),'DelayTime',1/25,'LoopCount',inf) % save gif
    disp([saveName '.gif  has ' num2str(numel(im)/10^6 ,4) ' Megapixels']) % Category:Animated GIF files exceeding the 50 MP limit
end


    function plotWeight(x,y,weight)
        % x [left right]
        % y [top bottom]
        % force in kN
        le = 0.11; % ropeLength
        density = 2500; % adjust
        % V = weight/density = 4/3*pi*r^3
        r = nthroot( (weight*3)/(density*4*pi), 3); % radius
        disc(x,       y-le-r/6,    r/3, RGB.weight,  RGB.black, 2.5*liSc) % handle (material)
        disc(x,       y-le-r/6,    r/6, RGB.white,   RGB.black, 2.5*liSc) % handle (hole)
        disc(x,       y-le-r*1.25, r,   RGB.weight,  RGB.black, 2.5*liSc) % body
        disc(x-r*0.4, y-le-r*0.75, r/3, RGB.weight2, 'none' , 1) % shine
        disc(x-r*0.45,y-le-r*0.7,  r/5, RGB.weight3, 'none' , 1) % glare
        text(x,       y-le-r*1.05,  num2str(weight,'%5.1f'),...
            'FontWeight','bold', 'FontName','Helvetica',...
            'Color',RGB.black, 'FontUnits','pixel', 'FontSize',200*r*liSc,...
            'HorizontalAlignment','center', 'VerticalAlignment','middle');
        text(x,       y-le-r*1.65, 'kg',...
            'FontWeight','bold', 'FontName','Helvetica',...
            'Color',RGB.black, 'FontUnits','pixel', 'FontSize',200*r*liSc,...
            'HorizontalAlignment','center', 'VerticalAlignment','middle');
        plot([x x],y+[0.03 -le-0.03],'Color',RGB.black,'LineWidth',6*liSc);
        
    end


    function bearing(x,y,sizze)
        % x coordinates of the center
        % y coordinates of the center
        % size
        plot([0 -0.5 0.5 0]*sizze+x,[0 -0.8660 -0.8660 0]*sizze+y,'k','LineWidth',5*liSc,'Color',RGB.black); % Triangle %  0.8660 = sqrt(3)*0.5
        plot([-0.7 0.7]*sizze+x,[-0.87 -0.87]*sizze+y,'k','LineWidth',5*liSc,'Color',RGB.black); % base line
        for iLine = -0.6:0.2:0.7
            plot(([-0.1 0.1]+iLine)*sizze+x,[-1.07 -0.87]*sizze+y,'k','LineWidth',3*liSc,'Color',RGB.black); % Hatching
        end
    end
end


function disc(x,y,r,colFa,colEd,linWi)
% x coordinates of the center
% y coordinates of the center
% r is the radius of the circle
% colFa FaceColor
% colEd EdgeColor
% linWi LineWidth
angleOffPoints = linspace(0,2*pi,300);
xc = x + r*sin(angleOffPoints);
yc = y + r*cos(angleOffPoints);
patch(xc,yc,'k','LineWidth',linWi,'EdgeColor',colEd,'FaceColor',colFa); % ,'FaceAlpha',0.2% transparencc does onl work in 'renderer','OpenGL' but that fails in combination with "getframe"
end


function plotBox(x,y,colFa,colEd,linw)
% x [left right]
% y [top bottom]
% colFa  face color  [r g b]
% colEd  edge color  [r g b]
% linw  line width
xs = [x(1)    x(1) x(2) x(2) x(1)    x(1)]; % x
ys = [mean(y) y(1) y(1) y(2) y(2) mean(y)]; % y joint in the middle of an edge to get nice corners
patch(xs,ys,colFa,'EdgeColor',colEd,'LineWidth',linw); %
end


function im = imReduceSize(im,redSize)
% Input:
%  im:      image, [imRows x imColumns x nChannel x nStack] (unit8)
%                      imRows, imColumns: must be divisible by redSize
%                      nChannel: usually 3 (RGB) or 1 (grey)
%                      nStack:   number of stacked images
%                                usually 1; >1 for animations
%  redSize: 2 = half the size (quarter of pixels)
%           3 = third the size (ninth of pixels)
%           ... and so on
% Output:
%  im:     [imRows/redSize x imColumns/redSize x nChannel x nStack] (unit8)
%
% an alternative is: imNew = imresize(im,1/reduceImage,'bilinear');
%        BUT 'bicubic' & 'bilinear'  produces fuzzy lines
%        IMHO this function produces nicer results as "imresize"
 
[nRow,nCol,nChannel,nStack] = size(im);

if redSize==1;  return;  end % nothing to do
if redSize~=round(abs(redSize));             error('"redSize" must be a positive integer');  end
if rem(nRow,redSize)~=0;     error('number of pixel-rows must be a multiple of "redSize"');  end
if rem(nCol,redSize)~=0;  error('number of pixel-columns must be a multiple of "redSize"');  end

nRowNew = nRow/redSize;
nColNew = nCol/redSize;

im = double(im).^2; % brightness rescaling from "linear to the human eye" to the "physics domain"; see youtube: /watch?v=LKnqECcg6Gw
im = reshape(im, nRow, redSize, nColNew*nChannel*nStack); % packets of width redSize, as columns next to each other
im = sum(im,2); % sum in all rows. Size of result: [nRow, 1, nColNew*nChannel]
im = permute(im, [3,1,2,4]); % move singleton-dimension-2 to dimension-3; transpose image. Size of result: [nColNew*nChannel, nRow, 1]
im = reshape(im, nColNew*nChannel*nStack, redSize, nRowNew); % packets of width redSize, as columns next to each other
im = sum(im,2); % sum in all rows. Size of result: [nColNew*nChannel, 1, nRowNew]
im = permute(im, [3,1,2,4]); % move singleton-dimension-2 to dimension-3; transpose image back. Size of result: [nRowNew, nColNew*nChannel, 1]
im = reshape(im, nRowNew, nColNew, nChannel, nStack); % putting all channels (rgb) back behind each other in the third dimension
im = uint8(sqrt(im./redSize^2)); % mean; re-normalize brightness: "scale linear to the human eye"; back in uint8
end


function map = createImMap(imRGB,nCol,startMap)
% createImMap creates a color-map including predefined colors.
% "rgb2ind" creates a map but there is no option to predefine some colors,
%         and it does not handle stacked images.
% Input:
%   imRGB:     image, [imRows x imColumns x 3(RGB) x nStack] (unit8)
%   nCol:      total number of colors the map should have, [integer]
%   startMap:  predefined colors; colormap format, [p x 3] (double)

imRGB = permute(imRGB,[1 2 4 3]); % step1; make unified column-image (handling possible nStack)
imRGBcolumn = reshape(imRGB,[],1,3,1); % step2; make unified column-image

fullMap = double(permute(imRGBcolumn,[1 3 2]))./255; % "column image" to color map 
[fullMap,~,imMapColumn] = unique(fullMap,'rows'); % find all unique colors; create indexed colormap-image
% "cmunique" could be used but is buggy and inconvenient because the output changes between "uint8" and "double"

nColFul = size(fullMap,1);
nColStart = size(startMap,1);
disp(['Number of colors: ' num2str(nColFul) ' (including ' num2str(nColStart) ' self defined)']);

if nCol<=nColStart;  error('Not enough colors');        end
if nCol>nColFul;   warning('More colors than needed');  end

isPreDefCol = false(size(imMapColumn)); % init
 
for iCol = 1:nColStart
    diff = sum(abs(fullMap-repmat(startMap(iCol,:),nColFul,1)),2); % difference between a predefined and all colors
    [mDiff,index] = min(diff); % find matching (or most similar) color
    if mDiff>0.05 % color handling is not precise
        warning(['Predefined color ' num2str(iCol) ' does not appear in image'])
        continue
    end
    isThisPreDefCol = imMapColumn==index; % find all pixel with predefined color
    disp([num2str(sum(isThisPreDefCol(:))) ' pixel have predefined color ' num2str(iCol)]);
    isPreDefCol = or(isPreDefCol,isThisPreDefCol); % combine with overall list
end
[~,mapAdditional] = rgb2ind(imRGBcolumn(~isPreDefCol,:,:),nCol-nColStart,'nodither'); % create map of remaining colors
map = [startMap;mapAdditional];
end

Licensing[edit]

I, the copyright holder of this work, hereby publish it under the following license:
Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current12:31, 19 November 2017Thumbnail for version as of 12:31, 19 November 2017500 × 450 (2.94 MB)Jahobr (talk | contribs)sign changed; text better
20:31, 1 November 2017Thumbnail for version as of 20:31, 1 November 2017500 × 450 (2.76 MB)Jahobr (talk | contribs)User created page with UploadWizard

File usage on other wikis

The following other wikis use this file: