editing and debugging matlab programs

5
Exercises: 1. >> z=x.^2+x Error message: Undefined function or variable 'x' Remedy: >>x=1 >> z=x.^2+x >>z=2 2. >>mod(7) Error message: Error using mod Not enough input arguments Remedy: >>mod(7,2) >>ans = 1 3. >>a=[1 2 3]; b= [ 1 4 7 ]; >>a*b Error message: Error using * Inner matrix dimensions must agree. Remedy: use elemental mulitiplication >>a.*b 4. >>a=[1 2 3]; b= [ 1 4 ]; >>a+b Error message: Error using + Matrix dimensions must agree. Remedy: Assign a vector and b vector of same size >> a=[1 2 3]; b= [ 1 4 7 ]; a+b >>ans = 2 6 510 5. >>a=[1,3] >>a(3) Error message: Index exceeds matrix dimensions. Remedy: assign a=[1:3] 6. >>a=[1:3] >>a(1.5) Error message: Subscript indices must either be real positive integers or logical. Remedy: a(1) or a(2) or a(3) 7. >> a=(1::,3) a=(1::,3)

Upload: apecevsb

Post on 12-Jul-2016

219 views

Category:

Documents


2 download

DESCRIPTION

Matlab and simulink lab

TRANSCRIPT

Page 1: Editing and Debugging MATLAB Programs

Exercises:

1. >> z=x.^2+xError message: Undefined function or variable 'x'Remedy:>>x=1>> z=x.^2+x>>z=2

2. >>mod(7)Error message: Error using mod Not enough input argumentsRemedy:>>mod(7,2)>>ans = 1

3. >>a=[1 2 3]; b= [ 1 4 7 ];>>a*bError message: Error using * Inner matrix dimensions must agree.Remedy: use elemental mulitiplication>>a.*b

4. >>a=[1 2 3]; b= [ 1 4 ];>>a+bError message: Error using + Matrix dimensions must agree.Remedy: Assign a vector and b vector of same size >> a=[1 2 3]; b= [ 1 4 7 ]; a+b>>ans = 2 6 510

5. >>a=[1,3]>>a(3)Error message: Index exceeds matrix dimensions.Remedy: assign a=[1:3]

6. >>a=[1:3]>>a(1.5)Error message: Subscript indices must either be real positive integers or logical.Remedy: a(1) or a(2) or a(3)

7. >> a=(1::,3) a=(1::,3) |Error: Unexpected MATLAB operator.

8. >> if a=3 if a=3 |Error: The expression to the left of the equals sign is not a valid target for an assignment.

Page 2: Editing and Debugging MATLAB Programs

Expt.No. Editing and Debugging MATLAB ProgramsDate:

Aim:

To know and understand editing and debugging MATLAB programs.

Softwares Required:

Matlab R2015a

Hardware Required:

A personnel Computer.

Procedures:

1. Open matlab File.2. Type the program in the editor window.3. Save the file with .m Extension and run the program.4. Verify the output in the command Window.

Theory:The different types of errors that are commonly occurred in matlab programming.

1 Arithmetic errors

2 Indexing errors

3 Assignment errors

4 Struct array errors

5 Syntax errors

5.1 Parenthesis errors

5.2 String errors

5.3 Other miscellaneous errors

6 Function Calling errors

7 Control Flow errors

8 Other errors

Page 3: Editing and Debugging MATLAB Programs

9. >> A = [1,2,3;4,5,6;7,8,9];A(2,:)=[3,5]Error: Subscripted assignment dimension mismatch.

10. >> x=[1 2 3:4 5 6:8 5 4];>> x(0)Error: Subscript indices must either be real positive integers or logicals.

11. >>ABS=[8 7 9 4 5 6]>>abs(1)>>ans =1Note:The element of array is different from what it shown.

12. >> a(1 a(1 |Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.

13. >> a(1))a(1)) |Error: Unbalanced or unexpected parenthesis or bracket.

14. >> eye 5Error using eyeCLASSNAME input must be a valid numeric class name.

15. >> a='hi a='hi |Error: String is not terminated properly.

16. >> 'AA'=='AAA'Error using == Error: Matrix dimensions must agree.Remedy: strcmp(‘AA’,’AAA’)

17. strcmp'AA','AAA'Error: Unexpected MATLAB expression.

18. a=1+Error: Expression or statement is incomplete or incorrect.

19. >> x=[2 3,3 5]>> eigen(x)Error: Undefined function or variable 'eigen'.

20. >> eig(x)Error using eigError: For standard eigenproblem EIG(A), A must be square.

Page 4: Editing and Debugging MATLAB Programs

Arithmetic Error: Attempting to take the inverse of a singular matrix will result in a warning and a matrix

of Infs. It is wise to calculate the determinant before attempting to take the inverse or, better, to use a method that does not require you to take the inverse since its not numerically stable.

Attempting to take a power of a nonsquare matrix results in the error. Matrix multiplication requires the number of columns in the first matrix to equal the

number of rows in the second. Otherwise, you get the error message.

Indexing Error:

Here the names of variables are case sensitive, but the names of functions are NOT. So if you make an array called Abs and you try to index abs(1), it will return 1 no matter what the first value in the array Abs is. Unfortunately, MATLAB will not return an error for this (although MATLAB v2008+ or so will return a warning saying that this will be changed in a later version), so a good rule of thumb is never ever name your variables the same as a function.

You cannot try to access a negative, complex, noninteger, or zero part of an array; if you do you get error message

Syntax errors:

Parenthesis errors:

Unlike in C++, you are not required to terminate every line with anything but a line break of some sort. However, there are still syntax rules you have to follow. In MATLAB you have to be especially careful with where you put your parenthesis so that MATLAB will do what you want it to.

Control Flow errors

The most common one by far is if you forget the 'END', which is an issue in M-file functions. It will tell you that 'at least one END is missing' and try to tell you where the loop or conditional statement starts.

If you have too many END statements and more than one function in an M-file, MATLAB may give you a cryptic message about not formatting the functions correctly. This is because all functions in the same M-file must either end with an END statement or not. It doesn't matter which, but if you have too many END statements in one of the functions, MATLAB will think your function is ending early and will get confused when the next function in line does not have an END statement at the end of it.

Result: