A Simple Matlab Based Calculator The aim of the project is t

A Simple Matlab Based Calculator The aim of the project is to expose students with Matlab programming environment. All parts of the project will be implemented in Matlab. 1. Description of the Project The project consists of two phases. Time-line and details are: a) Develop a basic calculator b) Add advanced features Phase 1: A skeleton Matlab code for the project is attached below. The code consists of two files, a main file (ProjectMain.m) for the project and a function file for implementing addition function of the calculator (fAddNumbers.m). The code provides a guide for you to develop your own code. Please create the main project file and add more function files in Matlab. Following functions are required to be performed by the calculator for scalar numbers: 1) Addition 2) Subtraction 3) Division 4) Multiplication For matrices and vectors, following operations are required 5) Logical operations (AND, NOT, OR, XOR) 6) Find the minimum, maximum in arrays with corresponding indices. Phase 2: Further progress on the code of phase-I to add advanced features: 1) Addition, subtraction, division and multiplication for matrices. The code should perform a check on matrix sizes and other related things. 2) 2-D plots. 3) Detailed error checking on the whole code if wrong inputs are given. The code should give appropriate response to guide the user to provide good (sensible) input. 4) Find roots of a given polynomial. 5) Any features which you thing should be added in the calculator, e.g., some symbolic calculations, 3-D plots, etc.

Solution

It was a REALLY LONG question but there it goes the code:

MAIN CODE:

clear all
clc
fprintf(\'CALCULATOR \ \ \');
fprintf(\'\ \');
fprintf(\'What operation would you like to do? \ \ \')
fprintf(\'\ \');
fprintf(\'1: Operation with scalar numbers (addition, substraction, division, multiplication)\ \ \')
fprintf(\'2: Operation with matrices and vectors (addition, substraction, division, multiplication)\ \ \')
fprintf(\'3: Logical operations (AND, NOT, OR, XOR)\ \ \')
fprintf(\'4: 2-D or 3-D plot of a function\ \ \')
op=input(\'Operation = \');

if op==1
scalar
elseif op==2
matrix
elseif op==3
logical
elseif op==4
plotting
else fprintf(\'Invalid operation \ \ \')
end

1) SCALAR OPERATIONS

clc
fprintf(\'You selected 1: operation with scalar numbers\ \ \')
fprintf(\'Insert the two numbers that are going to be used\ \ \')
A=input(\'A = \');
B=input(\'B = \');
fprintf(\'What operation would you like to do? \ \ \')
fprintf(\'\ \');
fprintf(\'1: Addition\ \ \')
fprintf(\'2: Substraction\ \ \')
fprintf(\'3: Division\ \ \')
fprintf(\'4: Multiplication\ \ \')
ops=input(\'Operation = \');
if ops==1
x=A+B;
fprintf(\'The result is\ \ \')
display(x);
elseif ops==2
x=A-B;
fprintf(\'The result is\ \ \')
display(x);
elseif ops==3
if B~=0
x=A/B;
fprintf(\'The result is\ \ \')
display(x);
else
fprintf(\'The operation cannot be made. Division by zero \ \ \')
end
elseif ops==4
x=A*B;
fprintf(\'The result is\ \ \')
display(x);
else fprintf(\'Invalid operation \ \ \')
end

2) MATRIX OPERATIONS

clc
fprintf(\'You selected 2: Operation with matrices and vectors \ \ \')
fprintf(\'Please, remember that every column is separated\ \ \')
fprintf(\'by a space while every row is separated by a semicolon\ \ \')
fprintf(\'For example:\ \ \')
fprintf(\'I=[1 2; 3 4]\ \ \')
I=[1 3; 3 4];
display(I);
fprintf(\'Insert the two vectors or matrix that are going to be used\ \ \')
A=input(\'A = \');
B=input(\'B = \');
fprintf(\'What operation would you like to do? \ \ \')
fprintf(\'\ \');
fprintf(\'1: Addition\ \ \')
fprintf(\'2: Substraction\ \ \')
fprintf(\'3: Division \ \ \')
fprintf(\'4: Multiplication\ \ \')
ops=input(\'Operation = \');
fprintf(\'\ \');
if ops==1
if size(A)==size(B)
x=A+B;
fprintf(\'The result is\ \ \')
display(x);
else
fprintf(\'The operation cannot be made. Matrix/Vectors dimensions must agree \ \ \')
end
elseif ops==2
if size(A)==size(B)
x=A-B;
fprintf(\'The result is\ \ \')
display(x);
else
fprintf(\'The operation cannot be made. Matrix/Vectors dimensions must agree \ \ \')
end
elseif ops==3
if size(A)==size(B) %columns of A equals to rows of B
x=A./B;
fprintf(\'Beware that, in order to avoid errors (infinite values) \ \ \')
fprintf(\'B matrix/vector cannot have any zero \ \ \')
fprintf(\'The result is\ \ \')
display(x);
else
fprintf(\'The operation cannot be made\ \ \')
end
elseif ops==4
D1=size(A); % rows and columns
D2=size(B);
if D1(2)==D2(1) %columns of A equals to rows of B
x=A*B;
fprintf(\'The result is\ \ \')
display(x);
else fprintf(\'Invalid operation \ \ \')
end
end

3) LOGICAL OPERATIONS

clc
fprintf(\'You selected 3: Logical operations \ \ \')
fprintf(\'Insert the two logical numbers or arrays that are going to be used\ \ \')
fprintf(\'In the case of arrays\ \ \')
fprintf(\'Please, remember that every column is separated\ \ \')
fprintf(\'by a space while every row is separated by a semicolon\ \ \')
fprintf(\'For example:\ \ \')
fprintf(\'I=[1 2; 3 4]\ \ \')
I=[1 3; 3 4];
display(I);
A=input(\'A = \');
B=input(\'B = \');
fprintf(\'What operation would you like to do? \ \ \')
fprintf(\'\ \');
fprintf(\'1: AND\ \ \')
fprintf(\'2: NOT\ \ \')
fprintf(\'3: OR\ \ \')
fprintf(\'4: XOR\ \ \')
ops=input(\'Operation = \');
if ops==1
x=and(A,B);
fprintf(\'The result is\ \ \')
display(x);
elseif ops==2
A1=not(A);
fprintf(\'not(A) \ \ \')
display(A1);
B1=not(B);
fprintf(\'not(B) \ \ \')
display(B1);
elseif ops==3
x=or(A,B);
fprintf(\'The result is\ \ \')
display(x);
elseif ops==4
x=xor(A,B);
fprintf(\'The result is\ \ \')
display(x);
else fprintf(\'Invalid operation \ \ \')
end

4) PLOTTING

clc
fprintf(\'You selected 4: 2-D or 3-D plot\ \ \')
fprintf(\'Insert the function that you want to plot as follows:\ \ \')
fprintf(\'In order to plot f(x), first the x array has to be defined\ \ \')
fprintf(\'x=-2:0.01:2; \ \ \')
fprintf(\'It means that x vector goes from -2 to 2, taking samples every 0.01 \ \ \')
fprintf(\'Then, the function f(x) is defined by another array\ \ \')
fprintf(\'y=sin(2*pi.*x); \ \ \')
fprintf(\'Beware of the dot that is used before multiplication symbol (before the x array)\ \ \')
fprintf(\'This dot only has to be used before multiplication symbol\ \ \')
fprintf(\'Thus, the function is already defined \ \ \')

fprintf(\'What kind of plot would you like to do? \ \ \')
fprintf(\'\ \');
fprintf(\'1: 2-D plot y=f(x)\ \ \')
fprintf(\'2: 3-D plot z=f(x,y)\ \ \')
opp=input(\'Operation = \');

if opp==1
fprintf(\'Insert de x array \ \ \')
x=input(\'x = \');
fprintf(\'\ \ \')
fprintf(\'Define the function \ \ \')
y=input(\'y = \');
plot(x,y);
elseif opp==2
fprintf(\'Insert de x array \ \ \')
x=input(\'x = \');
fprintf(\'\ \ \')
fprintf(\'Insert de y array \ \ \')
y=input(\'y = \');
fprintf(\'\ \ \')
fprintf(\'Define the function \ \ \')
z=input(\'z = \');
plot3(x,y,z);
else fprintf(\'Invalid operation \ \ \')
end

1), 2), 3) and 4) must be saved as the name they\'re called in the main code \"scalar.m\"; \"matrix.m\"; \"logical.m\" and \"plotting.m\", obviously in the same path (the same folder). I\'ve already tested the entire code and worked perfectly. Enjoy!

A Simple Matlab Based Calculator The aim of the project is to expose students with Matlab programming environment. All parts of the project will be implemented
A Simple Matlab Based Calculator The aim of the project is to expose students with Matlab programming environment. All parts of the project will be implemented
A Simple Matlab Based Calculator The aim of the project is to expose students with Matlab programming environment. All parts of the project will be implemented
A Simple Matlab Based Calculator The aim of the project is to expose students with Matlab programming environment. All parts of the project will be implemented

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site