0% found this document useful (0 votes)
18 views10 pages

MATLAB Array Operations Experiment

This document outlines an experiment in MATLAB that aims to perform scalar and array operations. It contains 6 questions to practice various array operations - accessing specific elements, determining array sizes and values, performing arithmetic on arrays, and solving a system of simultaneous equations. The conclusion restates that the experiment studied array operations in MATLAB through simulated practice questions.

Uploaded by

fbfcbbhx
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)
18 views10 pages

MATLAB Array Operations Experiment

This document outlines an experiment in MATLAB that aims to perform scalar and array operations. It contains 6 questions to practice various array operations - accessing specific elements, determining array sizes and values, performing arithmetic on arrays, and solving a system of simultaneous equations. The conclusion restates that the experiment studied array operations in MATLAB through simulated practice questions.

Uploaded by

fbfcbbhx
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

Date: 17/1/2022

Experiment 2
Aim: To perform scalar and array operations on MATLAB variables,
arrays, multi-dimensional arrays, sub-arrays.
Apparatus: MATLAB Software
Objective:
1. To learn how different library functions can be used with
arrays and multi-dimensional arrays.
2. To learn how specific elements of an array can be accessed
and utilized in coding.
Problems:
Q-1. Answer the following questions for the array shown here.

(a) Determine the size of an array1.


(b) Determine the value of an array1(1,4).
(c) Determine the size and value of an array1(:,1:2:5).
(d) Determine the size and value of an array1([1 3],end).
Q-2. Are the following MATLAB variable names legal or illegal? Why?
(a) dog1
(b) 1dog
(c) Do_you_know_the_way_to_san_jose
(d) _help
(e) What's_up?
Q-3. Determine the size and contents of the following arrays. Note that
the later arrays may depend on the definitions of arrays defined earlier
in this exercise.
(a) a = 2:3:8;
(b) b = [a' a' a'];
(c) c = b(1:2:3,1:2:3);
(d) d = a + b(2,:);
(e) w = [zeros(1,3) ones(3,1)' 3:5'];
(f ) b([1 3],2) = b([3 1],2);
(g) e = 1:-1:5;
Q-4. Assume that a, b, c, and d are defined as follows, and calculate
the results of the following operations in MATLAB if they are legal. If
an operation is illegal, explain why it is illegal.
(a) result = a + b;
(b) result = a * d;
(c) result = a .* d;
(d) result = a * c;
(e) result = a .* c;
(f ) result = a \ b;
(g) result = a .\ b;
(h) result = a .^ b;
Q-5. Evaluate each of the following expressions in MATLAB:
(a) 11 / 5 + 6
(b) (11 / 5) + 6
(c) 11 / (5 + 6)
(d) 3 ^ 2 ^ 3
(e) 3 ^ (2 ^ 3)
(f ) (3 ^ 2) ^ 3
(g) round(-11/5) + 6
(h) ceil(-11/5) + 6
(i) floor(-11/5) + 6
Q-6. Solve the following system of simultaneous equations for x:

-2.0 x1 + 5.0 x2 + 1.0 x3 + 3.0 x4 + 4.0 x5 - 1.0 x6 = 0.0


2.0 x1 - 1.0 x2 - 5.0 x3 - 2.0 x4 + 6.0 x5 + 4.0 x6 = 1.0
-1.0 x1 + 6.0 x2 - 4.0 x3 - 5.0 x4 + 3.0 x5 - 1.0 x6 = -6.0
4.0 x1 + 3.0 x2 - 6.0 x3 - 5.0 x4 - 2.0 x5 - 2.0 x6 = 10.0
-3.0 x1 + 6.0 x2 + 4.0 x3 + 2.0 x4 - 6.0 x5 + 4.0 x6 = -6.0
2.0 x1 + 4.0 x2 + 4.0 x3 + 4.0 x4 + 5.0 x5 - 4.0 x6 = -2.0

ANS

Q-7. The distance between two points (x1, y1) and (x2, y2) on a
Cartesian coordinate plane is given by the equation,
Write a program to calculate the distance between any two points (x1,
y1) and (x2, y2) specified by the user. Use good programming
practices in your program. Use the program to calculate the distance
between the points (-3, 2) and (3, -6).

Conclusion:
In this experiment, we studied about types of array, array
operations on MATLAB variables, arrays, multi-dimensional
arrays, sub-arrays. We simulated given questions and observed
results.

Common questions

Powered by AI

Writing a MATLAB program to calculate the distance between two points requires defining variables for coordinates, using the distance formula sqrt((x2-x1)^2 + (y2-y1)^2), and printing the result. Good practices include clear naming conventions, comments for code readability, validation checks for inputs, and modular design if applying in complex systems. It enhances maintainability and reduces bugs .

Advancing understanding of MATLAB for complex problems involves a multi-faceted approach: practicing diverse problems, utilizing official MATLAB documentation and forums for deep insights, and dissecting existing scripts to learn efficient patterns. Additionally, pursuing a structured course that delves into aspects like computational efficiency and algorithm design can provide context for optimization techniques .

Ensuring code flexibility in MATLAB involves using vectors and loops dynamically rather than hardcoding sizes, leveraging MATLAB's built-in functions for array manipulation, and incorporating error handling to catch unexpected inputs. Modular functions help separate concerns, making the code easier to update. Adopting these practices leads to adaptability for different array sizes or types .

A common method for solving a system of linear equations in MATLAB is using matrix representation to apply functions like 'linsolve' or the left division operator ('\'). Critical considerations include ensuring the coefficient matrix is square and checking that it is non-singular, which means it has a unique solution. Iterative methods could also be necessary for non-linear systems .

In MATLAB, variable names must start with a letter, followed by letters, digits, or underscores. Common illegal variable names include those starting with a number or containing special characters like '?' or punctuation marks. For instance, 'dog1' is legal, but '1dog' is illegal because it starts with a digit. Similarly, "What's_up?" is illegal due to the apostrophe and question mark .

MATLAB evaluates operators based on precedence; exponentiation ('^') occurs first, followed by multiplication and division ('*', '/'), and then addition and subtraction. Parentheses can override this order to ensure specific evaluations occur first. Incorrect assumptions about this order can lead to coding errors, affecting results as seen in evaluating expressions like '3 ^ 2 ^ 3' versus '3 ^ (2 ^ 3)' .

Element-wise operations in MATLAB are performed using the dot operator before an arithmetic symbol, such as .*, ./, or .^, which operates on corresponding elements of arrays. Matrix operations involve true matrix arithmetic like matrix multiplication (using *) and division (using /), following linear algebra rules. An operation may be illegal if dimensions don't align, such as multiplying a 1x3 and a 2x1 matrix using * instead of .* .

MATLAB follows operator precedence rules: exponentiation ('^') is evaluated before multiplication or division ('/' or '*'), and these are evaluated before addition or subtraction. Functions like 'round', 'ceil', and 'floor' apply to the result of their preceding calculations. For instance, 'round(-11/5) + 6' first computes -11/5, rounds it, and then adds 6 .

Multi-dimensional MATLAB arrays can be manipulated using indexing and functions like colon notation to extract sub-arrays. For example, from the array b = [a' a' a'], you can create a sub-array 'b([1 3],2)' which involves selecting specific rows and columns. Additionally, operations like 'b([1 3],2) = b([3 1],2)' swap certain elements in place to modify the array .

To determine the size of an array in MATLAB, you can use the 'size' function which returns the number of rows and columns in the array. For instance, size(array1) would return the dimensions of 'array1'. To access specific elements, you use parentheses with indices. For example, array1(1,4) would access the element in the first row and fourth column .

You might also like