File: ef230-2023-08/www/examples/physics_block_on_incline.m Download
%! A general solution for a box on incline with force and friction
%! Has routines for drawing straight and curved vectors
% University of Tennessee : Will Schleter : January, 2010

% Box of weight W on incline of angle A
% with force P applied at angle B (relative to incline)
% create a plot of Friction vs P, accel vs P, and a pseudo FBD

function main
clear all; close all; clc;
global g
gr=9.81;

% use a global structure to store parameters
g.Pmin=0;
g.Pmax=1000;
g.W=400;
g.mu_s=.5;
g.mu_k=.4;
g.A=20;
g.B=30;
drawFBD();

% parse the structure to create a prompts and values for inputdlg
% each must be a cell array of text values
prompts = fieldnames(g);
vals = cell(size(prompts));
for i=1:length(prompts);
    f=prompts{i};
    vals{i} = num2str(g.(f));
end
vals = inputdlg(prompts,'Inclined Box',1,vals);
if isempty(vals), return; end % cancelled

% put the input back into the structure
for i=1:length(prompts)
    f=prompts{i};
    g.(f)=str2double(vals{i});
end


% Generate a list of P values
P = g.Pmin:1:g.Pmax; % Applied force (N)

% normal force from FBDy
N = g.W.*cosd(g.A) - P.*sind(g.B);
N(N<0)=0; % N<0 means the block leaves the incline
Fmax = g.mu_s.*N;

% calculate F for all P's assuming ax=0 (from FBDx)
F = P.*cosd(g.B) - g.W.*sind(g.A);

% go thru for all F greater than Fmax and
% recalculate F to be the uk*N
for ii=1:1:length(P)
    if abs(F(ii))>abs(Fmax(ii))
        F(ii)=g.mu_k.*N(ii).*sign(F(ii)); %sign accounts for direction of F
    end
end
% FYI - these two lines are equivalent to the for loop above
% ii_slide = find(F>Fmax);
% F(ii_slide) = uk.*N(ii_slide).*sign(F(ii_slide));

% Now calc the acceleration in x
mass = g.W./gr;
ax = (P.*cosd(g.B) - g.W.*sind(g.A) - F)./mass;
    
subplot(2,2,2);
plot(P,ax,'b-');
title('Acceleration vs Applied Force');
xlabel('P (N)');
ylabel('Acceleration (m/s^2)');


subplot(2,2,4);
plot(P,F,'b-');
title('Friction vs Applied Force');
xlabel('P (N)');
ylabel('Friction (N)');

drawFBD()

% create and arrow with text label
function hlist = arrow(x,y,siz,col,lab)
if (length(x)<2) || (length(y)<2) || (length(x)~=length(y))
    errordlg('Error in input to arrow function');
    return;
end;
h1 = line(x,y,'color',col);
dx = x(end)-x(end-1);
dy = y(end)-y(end-1);
dd = norm([dx dy]);
dx = dx.*siz./dd;
dy = dy.*siz./dd;
ax(1)=x(end);
ay(1)=y(end);
ax(2)=ax(1)-dx+dy*.3;
ay(2)=ay(1)-dy-dx*.3;
ax(3)=ax(1)-dx-dy*.3;
ay(3)=ay(1)-dy+dx*.3;
h2 = patch(ax,ay,col,'edgecolor',col);
h3 = text(x(1),y(1),lab);
hlist = [h1 h2 h3];
return;


function drawFBD
global g
figure(1)
subplot(2,2,1)
cla;
axis equal
axis off
h=patch([0 0 6 6],[0 4 4 0],'g');
rax = [0 0 1];
ror = [0 0 0];
rotate(h,rax,g.A,ror);
arrlen = 2;
arrsiz = 0.5;
arrx = [0-arrlen.*cosd(g.B) 0 ];
arry = [4-arrlen.*sind(g.B) 4 ];
a = arrow(arrx,arry,arrsiz,'b','P');
rotate(a,rax,g.A,ror);

arrx = [3+arrlen.*cosd(90-g.A) 3 ];
arry = [4+arrlen.*sind(90-g.A) 4 ];
a = arrow(arrx,arry,arrsiz,'b','W');
rotate(a,rax,g.A,ror);

arrx = [3-arrlen.*cosd(90) 3 ];
arry = [0-arrlen.*sind(90) 0 ];
a = arrow(arrx,arry,arrsiz,'b','N');
rotate(a,rax,g.A,ror);

arrx = [6+arrlen.*cosd(0) 6 ];
arry = [0+arrlen.*sind(0) 0 ];
a = arrow(arrx,arry,arrsiz,'b','F');
rotate(a,rax,g.A,ror);

arrt = linspace(0,g.A*pi/180,20);
arrr = ones(size(arrt));
[arrx,arry] = pol2cart(arrt,arrr);
a = arrow(arrx,arry,.2,'r',['A=' num2str(g.A) '^o']);

arrt = pi+linspace(0,g.B.*pi/180,20);
arrr = ones(size(arrt));
[arrx,arry] = pol2cart(arrt,arrr);
arry = arry+4;
a = arrow(arrx,arry,.2,'r',['B=' num2str(g.B) '^o']);
rotate(a,rax,g.A,ror);