Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Sunday, September 6, 2020

Inventory Model Simulation

The overall objective of inventory (stock) control is to maintain inventory levels to that the total costs of holding stocks is minimise. A popular method of implementing stock control is through the use of inventory (stock) control charts and algorithms that automate the process.

Economic Order Quantity – EOQ Definition 
 
By Will Kenton
Reviewed By Janet Berry-Johnson
Updated Feb 10, 2020


What Is Economic Order Quantity (EOQ)?

Economic order quantity (EOQ) is the ideal order quantity a company should purchase to minimize inventory costs such as holding costs, shortage costs, and order costs. This production-scheduling model was developed in 1913 by Ford W. Harris and has been refined over time.1 The formula assumes that demand, ordering, and holding costs all remain constant.

The EOQ is a company's optimal order quantity that minimizes its total costs related to ordering, receiving, and holding inventory.
The EOQ formula is best applied in situations where demand, ordering, and holding costs remain constant over time.
Formula and Calculation of Economic Order Quantity (EOQ)

The formula for EOQ is:



​where: 

Q=EOQ units
D=Demand in units (typically on an annual basis)
S=Order cost (per purchase order)
H=Holding costs (per unit, per year)​

Matlab Simulation

The Simulation descibe a 300 days of stock. Each time the stock is at an Order Point, an Order of constant quantity is ordered to wholesaler.
The Quntity of Consuming the stock and the supply days change randomally in normal distribution.
The model take safety stock equal 2*sigma where sigma is the standard deviation of multipliction of the random variables : stock consume per day and days of supply.

Inventory Model
Inventory Model

Matlab Script:

% This Script Plot The Inventory Model of an item
pkg load statistics
clear

item_name = "Filter"

Main_ConsumePerDay = 40 % Main of items consume per day
Stdev_ConsumePerDay = 10 % Standard deviation of consume items per day

Main_SupplyDay = 10 % Main of the days to supply the item
Stdev_SupplyDay = 3   % Standard deviation of the days to supply the item

NumberDays = 300 % Number of days for simulation
N_STD = 2 % Number of standard deviation use for insurance inventory

% Calculate Quantity per Order
DaysPerYear = 250
interest_rate = 0.15 % The interest rate to hold stock

k = 300;    % Order Coast
item_price = 70 % Price of unit item
h = interest_rate * item_price  % price of holding one item per year
ConsumePerYear = DaysPerYear * Main_ConsumePerDay % Consume per Year

Optimal_QuntityOrder = round(sqrt(2*ConsumePerYear*k/h)); % Optimal Quntity Per Order
EOQ = Optimal_QuntityOrder % Economic Order Quantity
OrderExist = false

% Main consume during the supply period (in days)
MainConsumeSupplyDays = Main_ConsumePerDay*Main_SupplyDay

% standard ddeviation of consume during supply period
StdevConsumeSupplyDays = sqrt(Stdev_ConsumePerDay^2*Stdev_SupplyDay^2+
Stdev_ConsumePerDay^2*Main_SupplyDay^2+ Stdev_SupplyDay^2*Main_ConsumePerDay^2)

% Insurance inventory
InsureInventory = N_STD * StdevConsumeSupplyDays

% Order Point
OrderPoint = InsureInventory + MainConsumeSupplyDays %+ EOQ

StartStock = InsureInventory + EOQ

% Array of random consume per day of the utem
ConsumePerDayVal = normrnd(Main_ConsumePerDay, Stdev_ConsumePerDay,1, NumberDays);

stock(1) = StartStock;
i_order = 1 % index for order points
for i=1:NumberDays
  % Stock is under order point but no order yet
  if (stock(i) < OrderPoint) && (OrderExist == false)
    stock(i)
    OrderExist = true;
    j=1;
    N_SupplyDays = max(1, round(normrnd(Main_SupplyDay, Stdev_SupplyDay)))
    Xorder(i_order) = i;
    Yorder(i_order) = stock(i);
    i_order = i_order+1;
    
  % Stock is under order point and order exist
  elseif (stock(i) < OrderPoint)
    j=j+1;
    if (j-N_SupplyDays)>0.1
      OrderExist = false;
      stock(i) = stock(i) + EOQ;
    end
  end
  stock(i+1) = stock(i) - ConsumePerDayVal(i);

end

plot(stock(1:NumberDays))
axis([0 NumberDays 0 inf])
title('EOQ Model - 2*Sigma')
xlabel('Days')
ylabel('Stock')
hold
plot(Xorder, Yorder, 'ro')

grid
x(1:NumberDays) = 1:NumberDays;
y(1:NumberDays) = InsureInventory;

plot( y, 'b--')

legend('Stock','Order point', 'Insurance Stock Level')

 

Wednesday, July 29, 2020

Matlab - Move sinus function toward x axis direction

% The Script plot a Sinus Graph Functin and move it to the x axis direction.
x= 0:0.1:2*pi
y = sin(x);

for i1=0:630
    y = sin(x- i1/100);
    plot(x,y);
    drawnow;
    F(i1+1) = getframe;
end

% One can use the movie function to Play recorded movie frames
% To play the movie two times, use movie(F,2).

Friday, July 24, 2020

Matlab - Read data from Excel file and plot the data

This script use the matlab function readtable and table2array to read data from Excel file and plot the data.

Download the script read_xls.m

Data in Excel file:

Data in Excel file
Data in Excel file


The script:

% This script read data from Excel file
% and put the data in array and plot the data

% Read the data from Excel file Book1.xlsx
% and put the data in table variable t.
disp( "The data read from Book1.xlsx file")
t = readtable("C:\Users\etmos\Desktop\Book1.xlsx")

% Put the data from table variable  t  to variable x, y.
% "x" and "y" are the titles of the data un xlsx file.
x = table2array(t(:, "x"));
y = table2array(t(:, "y"));

% plot thr data.
plot(x,y)

Script Output

>> read_xls
The data read from Book1.xlsx file

t =

  20×2 table

     y     x
    ___    __

      1     1
      4     2
      9     3
     16     4
     25     5
     36     6
     49     7
     64     8
     81     9
    100    10
    121    11
    144    12
    169    13
    196    14
    225    15
    256    16
    289    17
    324    18
    361    19
    400    20

Data Graph



Thursday, July 2, 2020

Matlab - Nonlinear Differential Equation with Initial Condition

Nonlinear Differential Equation with Initial Condition

Solve this nonlinear differential equation with an initial condition. The equation has multiple solutions.
 
Download the file nonldiff1.m

% Nonlinear Differential Equation with Initial Condition
%Solve this nonlinear differential equation with an initial condition.
%The equation has multiple solutions.

% initial conditions:  y(t0) = y0

syms y(t) t0 y0
ode = (diff(y,t)+y)^2 == 1;
cond = y(t0) == y0;
ySol(t) = dsolve(ode,cond)


The matlab output:
 ySol(t) =

 exp(-t)*exp(t0)*(y0 + 1) - 1
 exp(-t)*exp(t0)*(y0 - 1) + 1


Wednesday, July 1, 2020

Demo of inputting a function at the input prompt

The script get function at the prompt and calculate the first and the second order derivative
of the function. The script can be run on Octave platform.
Download the file demo_prompt_function.m

%     Demo of inputting a function at the input prompt
%     and making an Anonymous function.

% This program. shows how to take a
% string input and make it into an anonymous function
% this uses the symbolic pkg.
% Load Symbolic pkg (Octave only)
pkg load symbolic
setenv PYTHON C:\ProgramData\Anaconda3\pythonw.exe

disp("Example input")
disp("x^2 + 3*x - 1 + 5*x*sin(x)")
str_fucn=input("please enter your function  ","s")
fucn_sym=sym(str_fucn)
f=function_handle(fucn_sym)
% now back to symbolic
syms x;
ff=formula(f(x));
% now calculate the derivative of the function
ffd=diff(ff);
% and convert it back to an Anonymous function
df=function_handle(ffd)
% now lets do the second derivative
ffdd=diff(ffd);
ddf=function_handle(ffdd)
% and now plot them all at interva (-2) - 2
x1=-2:.001:2;
plot(x1,f(x1),x1,df(x1),x1,ddf(x1))
grid minor on
legend("f","f '", "f '' ")

The result for input function cos(x)

Example input
x^2 + 3*x - 1 + 5*x*sin(x)
please enter your function  cos(x)
str_fucn = cos(x)
fucn_sym = (sym) cos(x)
f =

@(x) cos (x)

df =

@(x) -sin (x)

ddf =

@(x) -cos (x)
The Graph





Matlab - Plot simple graph

Plot y = sin(x) graph (Octave platform)
Download the file simple_graph.m

% Simple y=sin(x) graph plot

x= 0:0.01:2*pi; % x Vector of x values
y = sin(x);     % y vector ' y = sin(x)

plot(x,y)       % Plot the graph y = sin(x)

% Add tiltles and grid
xlabel('x')
ylabel('sin(x)')
title('Graph - y = sin(x)')
grid


The Graph

Plot y = sin(x) graph (Octave platform)

Tuesday, June 30, 2020

Matlab Script - Symbolic solution for second-order differential equation

This Script run on Octave platform. The script get input second order differential equation with initial condition, of the form:
 The script return the function y(t).
Download the file simplediff2.m 

% Script to solve  second-order differential equation

% Specify the second-order derivative by using:
% diff and the equation by using ==.
% Then, solve the equation by using dsolve.

% load symbolic package, and initilize PYTHON variable
pkg load symbolic
setenv PYTHON C:\ProgramData\Anaconda3\pythonw.exe

% Define symbolic variables:
% y(t) - function y and argumenr t
% a - parameter
% y0 - initial condition for y(t) -  y(0) = y0
% y1 - initial condition for y'(t) -  y'(0) = y1

syms y(t) a y0 y1

% ****  input the equaion here: *****
eqn = diff(y,t,2) == a*y;

% S = dsolve(eqn, cond)
S = dsolve (eqn, y(0) == y0, diff(y)(0) == y1)

Example :
Differential equation with initial condition:

% ****  input the equaion here: *****
eqn = diff(y,t,2) == a*y;

% S = dsolve(eqn, cond)
S = dsolve (eqn, y(0) == y0, diff(y)(0) == y1)

The Solution:

Matlab Script - Symbolic solution for first-order differential equation

This Script run on Octave platform. The script get input differential equation with initial condition, of the form:
 

 The script return the function y(t).
 Download the file simplediff1.m 

% simple script to solve
% Symbolic solution for first-order differential equation

% Specify the first-order derivative by using:
% diff and the equation by using ==.
% Then, solve the equation by using dsolve.

% load symbolic package, and initilize PYTHON variable
pkg load symbolic
setenv PYTHON C:\ProgramData\Anaconda3\pythonw.exe

% Define symbolic variables:
% y(t) - function y and argumenr t
% a - parameter
% y0 - initial condition for y(t) -  y(0) = y0

syms y(t) a y0 t0
% ****  input the equaion here: *****
eqn = diff(y,t) == a*y;

% ****  input the initial conditions here: *****
cond = [y(0)==y0];

S = dsolve(eqn, cond)


Examples 1:

Differential equation with initial condition:

 % ****  input the equaion here: *****
eqn = diff(y,t) == a*y;
% ****  input the initial conditions here: *****
cond = [y(0)==y0];

 The solution:




                                                                                                  

Examples 2:

Differential equation with initial condition:

% ****  input the equaion here: *****
eqn = diff(y,t) == a*t*y;

% ****  input the initial conditions here: *****
cond = [y(0)==y0];

 The solution:








Examples 3:

% ****  input the equaion here: *****
eqn = diff(y,t) == a*y;
% ****  input the initial conditions here: *****
cond = [y(t0)==y0];

 The solution:






Featured Post

Solve simple Absolute Value Inequality

  Exam - BARTON COLLEGE PRACTICE PLACEMENT TEST Ex. 39 Exercise: The inequality |8 - x| < 8   is equivalent to:   a) x < 0 b) x ...