[Go to site: main page, start]

0% found this document useful (0 votes)
13 views2 pages

Basic MATLAB Programming Guide

1) The document describes using MATLAB to simulate the trajectory of a ball hit at an initial velocity and angle to determine if it will score a six. 2) The simulation is set up using ode45 to solve the equations of motion with air drag, and the results are plotted and animated. 3) Basic MATLAB operations are also described such as defining matrices and vectors, using semicolons, and different types of loops.

Uploaded by

Himalaya Gaur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Basic MATLAB Programming Guide

1) The document describes using MATLAB to simulate the trajectory of a ball hit at an initial velocity and angle to determine if it will score a six. 2) The simulation is set up using ode45 to solve the equations of motion with air drag, and the results are plotted and animated. 3) Basic MATLAB operations are also described such as defining matrices and vectors, using semicolons, and different types of loops.

Uploaded by

Himalaya Gaur
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Basic Programming In [tOut, XOut] = ode45(@balltrajectoryfun,

tspan, X0, [], param);


MATLAB
%%displaying the results
 Book by Laurene Figure(1);
Plot(XOut(:,1), XOut(:,2), ‘bo’);
Fausett Xlabel(‘x(m)’); ylabel(‘y(m)’);

%%animating results
Exitcode = ballAnimation(tOut, Xout);
To start a MATLAB editor we have to give
a command edit space followed by the file
name.
This will be the blank file.

Basic Data Types


Matlab easily works with arrays
MATLAB programming Scalars, vectors and arrays
Example Assigning variables
Indian captain, Msd hits a ball with initial Row vs column vectors
velocity of 35m/s and angle of 45 degree. Arrays/matrices
If the boundary is at a distance of 75m, Variable are case-sensitive
will he score a six.
How to do it in MATLAB Command
%%setting up a problem Window
V_net = 35m/s ; U_o=Vcostheta
where theta is pie/4 and V_o = >> a = 4. Gives a = 4
Vsintheta >> b = [1:5]. Gives b = 1 2 3
4 5. (an array from 1 to 5)
MATLAB Code >>c = [1, 2; 7, 4]. Gives c = 1 2
%%Define parameters and initial ( a 2x2 martix)
conditions 7 4
Param.g = 9.8; % gravitational acceleration
[Link] = 0.006. %air drag
coefficient >>d = [1;2;3;4]. Gives a column
U_o = 35*cos(pi/4); %x-direction velocity vector with values 1,2,3,4.
V_o = 35*sin(pi/4) ; %y-direction velocity
Comma are going to separate
various elements in a particular
%%setting up and solving the problem
rows where as semicolon is going to
X0 = [0; 0; U_0; V_0]; %starting position
is the origin separate elements in various rows
tSpan = [0,20]; %simulation time
When we end a command with a [Link] LOOP
semicolon then echo is being
suppress i.e. variable is assign to the
Command
function. (matlab turant result shoe While i<10
nhi karega ) <statement 1>;
.
To clear a variable from command
window, just write clear space .
variable name <statement n>;
I=i+1;
Array Operation In MATLAB
End
To write identity matrix of size 3x3
write When to use for loop?
>> c = eye(3) For loop is used when a set
Transpose of matrix = use an of operations are to be
apostphi ‘ repeated a specific number
Getting any particular element of a of time.E.g; find first 10
any size matrix, let us take a 3x3
matrix F and we have find element
terms of Fibonacci series
(2,1) i.e. matrix name( row no. , Find factorial of number n
column no)
F(2.1) give that element

Various LOOP in MATLAB


1: For Loop
Command
For i=1:10
<statement 1>;
.
.
<statement n>;
End

Common questions

Powered by AI

In MATLAB, appending a semicolon to the end of a command suppresses the output of the command in the Command Window. This is useful for improving the efficiency of scripts and functions by preventing unnecessary clutter from being displayed. Suppressing output is particularly important when working with large datasets or matrices, where displaying everything could slow down computation and overwhelm the interface .

ODE45 is a MATLAB function used to solve ordinary differential equations (ODEs) using a variable-step, variable-order numerical solver based on the Dormand-Prince pair of formulas. It is particularly effective for non-stiff differential equations. To use ODE45, the system of equations must be expressed as a function handle, and initial conditions along with a time span must be specified. ODE45 adjusts the step size dynamically to provide accurate solutions efficiently. It is commonly used in simulations of physical systems, where precise modeling of time-variant behavior is necessary, such as in trajectory planning or dynamic systems analysis .

Scalar operations in MATLAB refer to computations where single numeric values (scalars) are used in operations or functions, either with other scalars or extended to elements throughout a matrix or array. Scalars can act as constants in calculations, parameters in functions, or initial values in iterative procedures. Their importance lies in their simplicity and efficiency, as they allow straightforward, element-wise operations without complexity, which is central in many mathematical computations and algorithms involving linear algebra or differential equations, ensuring consistency in mathematical operations .

In MATLAB, arrays can be one-dimensional (vectors) or multi-dimensional, whereas matrices are 2-dimensional arrays that follow specific algebraic rules. Assigning arrays involves listing elements linearly, while matrices require clarifying the row and column structure. Syntax differs, as commas separate elements in rows and semicolons separate rows. The implications on mathematical functions include differences in applicable operations: arrays allow element-wise operations, enabling broad applications in element-by-element computation, while matrices invoke linear algebra functions, necessitating adherence to row-column operations for matrix multiplication, inversion, and decomposition. Recognizing these distinctions is crucial for leveraging MATLAB’s computational capabilities efficiently .

To determine if the ball will score a six using MATLAB, you need to set up and solve the projectile motion problem. Define the initial conditions: initial velocity (V_net = 35 m/s), launch angle (45 degrees), and use standard gravitational acceleration (g = 9.8 m/s²) and air resistance coefficient (kappa = 0.006). Calculate the x and y components of the velocity: U_o = 35*cos(pi/4) for x-direction, and V_o = 35*sin(pi/4) for y-direction. Set the initial position as origin and use the ODE45 function to solve the trajectory over a defined time span (e.g., tSpan = [0, 20]). Plot the trajectory using the plot function and check if the x-coordinate at y=0 (when the ball returns to ground level) exceeds 75 meters .

In MATLAB, row vectors and column vectors are arrays with different orientations. A row vector is a 1-by-n array created by listing elements separated by commas or spaces within square brackets. For example, >> row_vector = [1, 2, 3]. A column vector is an n-by-1 array created by separating elements with semicolons within square brackets or by transposing a row vector with an apostrophe. For example, >> column_vector = [1; 2; 3] or >> column_vector = [1, 2, 3]'. The main difference lies in their dimensional arrangements, impacting certain operational behavior in matrix calculations .

An identity matrix is a square matrix with ones on its main diagonal and zeros elsewhere. In MATLAB, you can create an identity matrix of size n by n using the eye function: >> identity_matrix = eye(n). The identity matrix is significant in linear algebra because it serves as the multiplicative identity in matrix multiplication, meaning any matrix multiplied by the identity matrix remains unchanged. It is foundational in algorithms that require a neutral element, such as solving matrix equations or finding matrix inverses .

In MATLAB, variable names are case-sensitive, meaning that variables with names differing only by case (e.g., 'Variable' vs. 'variable') are treated as distinct entities. This can lead to errors if variables are accidentally called with incorrect casing. Best practices to avoid issues include maintaining consistent naming conventions, such as using camelCase or all lower/uppercase, and refactoring code to replace magic numbers or temporary variables with descriptively named constants or variables .

In MATLAB, a FOR loop is employed when the number of iterations is known beforehand, making it suitable for tasks like iterating over elements in a fixed-size dataset or sequence, such as generating the first n terms of a series. It has the structure: for index = values, with statements enclosed between 'for' and 'end'. In contrast, a WHILE loop is used when the termination condition is met during execution, useful in scenarios where the number of iterations depends on dynamically changing conditions or events. It uses the structure: while condition, with statements between 'while' and 'end'. Each type of loop is chosen based on predictability of the iteration count and logical flow requirements .

In projectile motion, air resistance, often modeled with a drag coefficient, opposes the motion of an object, reducing its range and altering its trajectory from the ideal parabolic path observed without drag. In MATLAB, air resistance can be accounted for by including a drag coefficient (e.g., Param.kappa = 0.006) in the differential equations governing the projectile's motion. The equations must be modified to include terms that represent the resistive forces proportional to velocity, consequently solved using numerical methods like ODE45 to accommodate the non-linear characteristics introduced by drag .

You might also like