0% found this document useful (0 votes)
28 views

Numerical Analysis Lab 1

Uploaded by

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

Numerical Analysis Lab 1

Uploaded by

waleedasghar677
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Numerical Analysis Lab

Experiment # 1
Objective: Introduction to MATLAB and Numerical Analysis Basics
Overview: MATLAB interface, command window, script editor, and workspace.
Command Window: This is the main area where we can type and execute individual commands.
It's ideal for quick calculations, testing small pieces of code, and interacting directly with
MATLAB.
Script Editor: This is where we can write, save, and run multiple lines of code, known as scripts.
Scripts are saved with a .m extension and allow us to organize code in a structured way.
Workspace: The Workspace shows all the variables we’ve defined or imported. We can view each
variable’s size, data type, and value. This helps us keep track of our data and see the results of our
calculations.
Basic Commands: Variables, arithmetic operations, and built-in functions (e.g., sin, cos, exp).
In MATLAB, we can perform a variety of calculations and define variables easily. Here are some
common operations:

Variables: We assign values to variables using the = symbol. For example:

x = 5; % Defines a variable x with a value of 5


y = 3.14; % Defines a variable y with a value of 3.14

We can name variables using letters, numbers, and underscores, but variable names cannot start
with a number.

Arithmetic Operations: MATLAB supports standard arithmetic operations:

• Addition: +
• Subtraction: -
• Multiplication: *
• Division: /
• Exponentiation: ^

Example:

% Define variables
a = 10;
b = 5;

% Perform arithmetic operations


addition = a + b; % Addition
power = a ^ 2; % Exponentiation
% Display results
disp(['Addition: ', num2str(addition)]);
disp(['Power: ', num2str(power)]);

Built-in Functions: MATLAB has a large library of built-in functions for mathematical
operations. Examples:

• sin(x): Calculates the sine of x.


• cos(x): Calculates the cosine of x.
• exp(x): Calculates the exponential of x (e^x).

Vectors and Matrices:

Vectors and matrices are fundamental to MATLAB and are easy for us to work with:

• Defining a Vector:
o Row vector: v = [1, 2, 3, 4];
o Column vector: v = [1; 2; 3; 4];
• Defining a Matrix:

A = [1, 2; 3, 4]; % A 2x2 matrix

Plotting: Basic 2D plotting (e.g., plot, xlabel, ylabel, title) for visualizing functions and data.

MATLAB has powerful plotting capabilities, and basic 2D plotting is straightforward:

• Creating a Plot: To plot a function or data, we use the plot command. For example:
• Labels and Titles:
o We use xlabel and ylabel to label the x and y axes.
o We use title to add a title to the plot.

Example:

% Define the range of x values


x = linspace(0, 2*pi, 100); % 100 points from 0 to 2π

% Calculate the sine of each x value


y = sin(x);

% Create the plot


plot(x, y);

% Add labels and title


xlabel('Angle (radians)'); % Label for x-axis
ylabel('Sine Value'); % Label for y-axis
title('Plot of the Sine Function'); % Title for the plot

You might also like