Welcome to the World of Modelling and Simulation

What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and ...

Newton-Raphson Method Codes for MATLAB

Newton-Raphson method is implemented here to determine the roots of a function. This algorithm is coded in MATLAB m-file. There are three files: func.m, dfunc.m and newtonraphson.m. The func.m defines the function, dfunc.m defines the derivative of the function and newtonraphson.m applies the Newton-Raphson method to determine the roots of a function.

Evaluating the root of sin(x):

func.m file
% defines the function as its roots to be determined

function [solution]=func(x)
solution=sin(x);

dfunc.m file
% defines the derivative of the function

function [dsolution]=dfunc(x)
dsolution=cos(x);

newtonraphson.m file
% applies Newton Raphson algorithm to determine the roots of a function

close all;
clear all;
x = input('Starting guess = ');
tolerance = 1e-8;
iterations = 0;
while (iterations<100) && (abs(func(x))>tolerance)
x = x-(func(x)/dfunc(x));
iterations = iterations + 1;
end
if iterations==100
disp('No root found')
else
disp(x)
end


Output:

Starting guess = 1

2.9236e-13

Starting guess = 0.1

1.2495e-11

Starting guess = 0.01

1.2335e-20


Evaluating the root of x-2*sin(xˆ2):

func.m file
% defines the function as it's roots to be determined

function [solution]=func(x)
solution = x-(2*sin(x.^2));

dfunc.m file
% defines the derivative of the function

function [dsolution]=dfunc(x)
dsolution=1-(4*x*cos(x));


Output:

Starting guess = 0.1

-1.0489e-10

Starting guess = 0.3

-2.4565e-10

Starting guess = 1

0.5055


Evaluating the root of x^2-5:

func.m file
% defines the function as it's roots to be determined

function [solution]=func(x)
solution = x^2-5;

dfunc.m file
% defines the derivative of the function

function [dsolution]=dfunc(x)
dsolution = (2*x);


Output:

Starting guess = 2
 
2.2361

Starting guess = 3

2.2361

Starting guess = 0.01

2.2361

#NewtonRaphson

Exploring Some MATLAB Basic Commands

In this post, we will explore some of the basic built-in MATLAB commands.

Generation of Random Numbers (“rand” command):

The thousand random numbers are generated by the following MATLAB command. In the following table 1, 100 of the 1000 generated random numbers are shown.

>> a=rand(1000,1)

Random Numbers














Random Number Sorting:

>> b=sort(a)
The sorted numbers (100 out of the 1000) are shown in table 2.

Random Number Sorting

























>> c=mean(b)
c = 0.5089
   
>> d=median(b)
d = 0.5162
   
>> e=mode(b)
e = 3.4146e-04
  
>> f=var(b)
f = 0.0818
   
>> g=std(b)
g = 0.2860

>> h=bar(b)
h = 174.0016


The standard uniform distribution is the simplest distribution for continuous random variable.  In this case the probability of occurrence of each event is same and when the probability of occurrence is plotted against the numbers, it gives a horizontal line.  In standard uniform distribution, the lowest boundary is 0 and the highest boundary is 1. So, the area under the horizontal line always gives 1. The distribution is rectangular in shape which is defined by maximum and minimum values.


Showing the bar plot of the uniformly distributed random numbers





















Figure 1: Showing the bar plot of the uniformly distributed random numbers.


Generation of Random Numbers (“randn” command):

The thousand random numbers are generated by the following MATLAB command. In the following table 3, 100 of the 1000 generated random numbers are shown.

>>a=randn(1000,1)

Random number generation

























>>b=sort(a)

Random number generation

























>> c=mean(b)
c =    0.0512


>> d=median(b)
d =   0.0507


>> e=mode(b)
e = -3.4915


>> f=var(b)
f = 1.0212

 
>> g=std(b)
g = 1.0105

   
>> h=bar(b)
h = 174.0031

The standard normal distribution is probably the most useful tool in solving many problems. It is a bell shaped curve and has a peak value at its center. This distribution is symmetric about the mean and also asymptotic (the curve gets closer and closer to the x-axis however never touches the axis). The total area under the curve is 1. The position of a normal distribution is determined by the mean and standard deviation as well.


normally distributed random numbers


















Figure 2: Showing the bar plot of the normally distributed random numbers

What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and civil engineering. It is concerned about the theories and applications of physics based modelling, for example analytical approaches, finite element method, etc., numerical methods to solve problems, and so forth. Nevertheless, the concept may be applied to other research arenas as well. Now-a-days, modelling is the very first step in making an object to be used in some applications as this would provide valuable insight in the design as well as its physical characteristics.

Modelling and Simulation Examples
Modelling may be classified broadly into two categories: signal based and model based approach. Signal based method is a well-established technique that has been implemented for a long time. This method has simplicity as it only depends on the system outputs for fault diagnosis while treating the plant as black box. On contrary, model based approach creates a model of the system with physical parameters where any change in the model can easily be observed by tuning those parameters. Model based approach may be classified into two categories - data driven and physics based modelling.

Designed in SOLIDWORKS









In this blog, we will see mostly physics based modelling examples using various methods. There are also demonstrations of various software, such as, MATLAB, Mathematica, SolidWorks, ABAQUS, and so on. We will also discuss some basics of other modelling methods.


#Blog #Blogger #Modelling #ModellingSimulation #PhysicsBasedModels #NumericalMethod