You are on page 1of 17

Lab Report 1

USE MATLAB/SIMULINK TO SOLVE MATHEMATICAL PROBLEMS


Name Lab Date: Nguyen Van Sinh 15/05/2011 Date of Birth: 24/09/1990 Repetition:

Introduction The purpose of this lab Using Matlab to solve equation, linear equations system Practice of operations on matrix, vector by using Matlab Generating and plot functions on Matlab Performing some techniques on graph

Hypothesis The outcomes of the experiment which I want to get In this lab, by using Matlab, I create Matlab-Scripts or m-files to solve some equations, equations system. I know ways to represent functions, use functions and connect those functions. In creation of m-file, I work with support commands. I know the ways so that I can find and directly use commands in library of Matlab to solve equation, equations system. I can represent vector, matrix of m n size and perform many operations on matrix and vector with many commands in packet or library of Matlab. I can represent and plot some functions and work on graph with some technique such as create title and name of axis, fill color, make comment windows and notes

Procedures and Results

Steps that I took to accomplish this lab assignment and I show results

1. Exercises 1:
In this exercise, I created m-file to solve equation, after that I find command to solve equation.

For m-file: clc; disp('Program of solving the quadratic equation of the form ax^2 + bx + c = 0'); disp('Author: Nguyen Van Sinh'); fprintf('Class: K53D\n\n'); while (true) clear; disp('Enter a,b,c'); a = input('a = '); b = input('b = '); c = input ('c = '); fprintf('Equation is: %.2f*x^2 + %.2f*x + %.2f = 0\n',a,b,c); delta = b^2 - 4*a*c; if delta < 0 disp('Solutions of equation:'); fprintf('x1 = %.2f + j*%.2f;\n', (b/(2*a)),sqrt(abs(delta))/(2*a)); fprintf('x2 = %.2f - j*%.2f;\n', (b/(2*a)),sqrt(abs(delta))/(2*a)); elseif delta == 0 disp('Solutions of equation: '); x= -(b/(2*a)); fprintf('x1 = x2 = %.2f\n',x); else disp('Solutions of equation:'); x1=(-b+sqrt(delta))/(2*a); x2=(-b-sqrt(delta))/(2*a); fprintf(' x1 = %.2f;\n x2 = %.2f\n', x1,x2); end reply = input('Do you want more? Y/N: ', 's'); if reply == 'y' || reply == 'Y' fprintf('\n\n'); continue; else disp('Done! Thank you for using my program'); break; end end

From function: Function of sol_eq_2: function [x1, x2] = sol_eq_2(a, b, c) fprintf('Equation is: '); fprintf('%.2f*x^2 + %.2f*x + %.2f = 0\n',a,b,c); delta = b^2 - 4*a*c; if delta < 0 disp('Solutions of equation:'); x1 = (-b + i*sqrt(abs(delta)))/(2*a); x2 = (-b - i*sqrt(abs(delta)))/(2*a); elseif delta == 0 disp('Equation has solution:'); x1 = -(b/(2*a)); x2 = x1; else disp('Solutions of equation:'); x1=(-b+sqrt(delta))/(2*a); x2=(-b-sqrt(delta))/(2*a); end end Function of main_sol_eq_2: clc; disp('Program of solving the quadratic equation of the form ax2 + bx + c = 0'); disp('Author: Nguyen Van Sinh'); fprintf('Class: K53D\n\n'); while (true) clear; disp('Enter a,b,c'); a = input('a = '); b = input('b = '); c = input ('c = '); [x1, x2] = sol_eq_2(a, b, c); disp(x1); disp(x2); %----if you want to reuse reply = input('Do you want more? Y/N: ', 's'); if reply == 'y' || reply == 'Y' fprintf('\n\n'); continue; else disp('Done! Thank you for using my program'); break; end end

The result of exercises 1: Program of solving the quadratic equation of the form ax^2 + bx + c = 0 Author: Nguyen Van Sinh Class: K53D Enter a,b,c a=1 b=3 c=2 Equation is: 1.00*x^2 + 3.00*x + 2.00 = 0 Solutions of equation: x1 = -1.00; x2 = -2.00 Do you want more? Y/N: y

Enter a,b,c a=1 b = -2 c=1 Equation is: 1.00*x^2 + -2.00*x + 1.00 = 0 Solutions of equation: x1 = x2 = 1.00 Do you want more? Y/N: Y

Enter a,b,c a=1 b=1 c=1 Equation is: 1.00*x^2 + 1.00*x + 1.00 = 0 Solutions of equation: x1 = -0.50 + j*0.87; x2 = -0.50 - j*0.87; Do you want more? Y/N: n Done! Thank you for using my program >>

For using command: solve >> solve('x^2 + 3*x + 1 = 0') ans = - 5^(1/2)/2 - 3/2 5^(1/2)/2 - 3/2 >> solve('x^2 + 2*x + 1 = 0') ans = -1 -1 >> solve('x^2 + x + 1 = 0') ans = - (3^(1/2)*i)/2 - 1/2 (3^(1/2)*i)/2 - 1/2 >>

2. Exercises 2:
Command of Generating Vector in Matlab and results: >> v = [1 2 3 4 5] v= 1 2 3 4 5

>> v = 0:0.1:5 v= Columns 1 through 12 0 0.1000 0.2000 0.3000 0.9000 1.0000 1.1000 Columns 13 through 24 1.2000 1.3000 1.4000 2.1000 2.2000 2.3000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 0.4000 0.5000 0.6000 0.7000 0.8000

Columns 25 through 36 2.4000 2.5000 2.6000 3.3000 3.4000 3.5000 Columns 37 through 48 3.6000 3.7000 3.8000 4.5000 4.6000 4.7000 Columns 49 through 51 4.8000 4.9000 5.0000 >> v = [3; -1; 2] v= 3 -1 2 >> v = 10:-0.1:8 v= Columns 1 through 12 10.0000 9.9000 9.8000 9.7000 9.1000 9.0000 8.9000 Columns 13 through 21 8.8000 8.7000 8.6000 8.5000 8.4000 8.3000 8.2000 8.1000 8.0000 9.6000 9.5000 9.4000 9.3000 9.2000 3.9000 4.0000 4.1000 4.2000 4.3000 4.4000 2.7000 2.8000 2.9000 3.0000 3.1000 3.2000

>> >> v1 = 1:3; >> v2 = 4:6; >> v1*v2 ??? Error using ==> mtimes Inner matrix dimensions must agree.

>> v1.*v2 ans = 4 10 18 >> v1'*v2 ans = 4 5 6 8 10 12 12 15 18 >> v1'+v2 ??? Error using ==> plus Matrix dimensions must agree. >> inv(v1) ??? Error using ==> inv Matrix must be square. >> inv(v2) ??? Error using ==> inv Matrix must be square.

3. Exercises 3: Vector operation


a. >> v = 0:99; >> sum(v) ans = 4950 b. >> v1 = v.*v; >> sum(v1) ans = 328350 c. >> v2 = v*pi/14; >> v2 = sin(v*pi/14); >> sum(v2) ans = 8.6527

4. Exercises 4:
a. Define the matrices and vectors >> A = [2 3 4;5 6 7;8 9 1] A= 2 5 8 3 6 9 4 7 1

>> B = [1 -2;-3 4;5 -6] B= 1 -2 -3 4 5 -6 >> c = [1;-2;0] c= 1 -2 0 >> D = [-1;4;2] D= -1 4 2 b. Calculate >> A*B ans = 13 -16 22 -28 -14 14

>> A*c ans = -4 -7 -10 >> B' ans = 1 -3 -2 4 >> inv(A) ans = -2.1111 1.2222 -0.1111 1.8889 -1.1111 0.2222 -0.1111 0.2222 -0.1111 >> A^-1 ans = -2.1111 1.2222 -0.1111 1.8889 -1.1111 0.2222 -0.1111 0.2222 -0.1111 c. Define matrix C with the help of matrix A and B >> C = []; >> C = [C A]; >> C = [C B] C= 2 5 8 3 6 9 4 7 1 1 -2 -3 4 5 -6 5 -6

Find all elements whose value is greater than 4 >> C = C(C>4) C= 5 8 6 9 7 5 d. Generate matrix of 34 with random value between 0 and 1 >> m = rand(3,4) m= 0.7513 0.6991 0.5472 0.2551 0.8909 0.1386 0.5060 0.9593 0.1493 0.2575 0.8407 0.2543

Replace all elements that are smaller than 0.5 by 0 >> m(m<0.5) = 0 m= 0.7513 0.6991 0.5472 0 0 0.8909 0 0.8407 0.5060 0.9593 0 0

5. Exercises 5:
>> t = linspace(-pi,pi,10000);

>> plot(t,sin(2*pi*t));

>>plot(t,tan(2*pi*t));

>> plot(t,cos(2*pi*t));

6. Exercises 6: Linear system


In this exercise, I use command of solve to solve equations system >> [x1,x2, x3] = solve('3*x1 + 2*x2 + x3 = 0','4*x1 + x2 + 3*x3 = 0','x1 + 2*x3 = 0') x1 = 0

x2 = 0

x3 = 0

7. Exercises 7:
In this exercise, I combine parts of a, b, and c into m-file clear, clc; disp('Program of drawing circle in x-y coordinates'); disp('Author: Nguyen Van Sinh'); disp('Class: K53D'); disp('-------------------------'); while(true) %part a disp('Circle of radius R equation: (x - a)^2 + (y b)^2 = R^2'); disp('Enter center C(a, b) of circle: '); a = input( 'a = '); b = input ('b = '); R = input ('Enter radius of circle R = '); fprintf('Equation become: (x - %.2f)^2 + (y %.2f)^2 = %.2f\n', a,b,R^2); t = linspace(0,2*pi,1000);%t = 0:0.0001:2*pi; x = R*cos(t) + a; y = R*sin(t) + b; plot(x,y),grid; %part b grey = [0.4 0.4 0.4]; disp('1: Blue 2: Green 3: Red 4: Cyan'); disp('5: Magenta 6: Yellow 7: Black 8: Grey'); color = input('Enter color(number follow above): '); if color == 1 fill(x,y,'b'); elseif color == 2 fill(x,y,'g'); elseif color == 3 fill(x,y,'r'); elseif color == 4 fill(x,y,'c'); elseif color == 5 fill(x,y,'m'); elseif color == 6 fill(x,y,'y'); elseif color == 7 fill(x,y,'k'); elseif color == 8 fill(x,y,grey); end hold on; %part c title(['\fontsize{11}\color[rgb]{0 .5 .5}Circle with

center C(',num2str(a),',',num2str(b),') and radius R = ',num2str(R)]); xlabel(['\fontsize{12}\color[rgb]{0 .5 .5}X']); ylabel(['\fontsize{12}\color[rgb]{0 .5 .5}Y']); reply = input('Do you want more? Y/N: ', 's'); if reply == 'y' || reply == 'Y' fprintf('----------------------------------\n\n'); continue; else disp('Done! Thank you for using my program'); break; end end legend('Color: Grey');

My result:
Program of drawing circle in x-y coordinates Author: Nguyen Van Sinh Class: K53D ------------------------Circle of radius R equation: (x - a)^2 + (y - b)^2 = R^2 Enter center C(a, b) of circle: a = 0 b = 0 Enter radius of circle R = 1 Equation become: (x - 0.00)^2 + (y - 0.00)^2 = 1.00 1: Blue 2: Green 3: Red 4: Cyan 5: Magenta 6: Yellow 7: Black 8: Grey Enter color(number follow above): 8 Do you want more? Y/N: n Done! Thank you for using my program

Others:
Program of drawing circle in x-y coordinates Author: Nguyen Van Sinh Class: K53D ------------------------Circle of radius R equation: (x - a)^2 + (y - b)^2 = R^2 Enter center C(a, b) of circle: a = 0 b = 0 Enter radius of circle R = 2 Equation become: (x - 0.00)^2 + (y - 0.00)^2 = 4.00 1: Blue 2: Green 3: Red 4: Cyan 5: Magenta 6: Yellow 7: Black 8: Grey Enter color(number follow above): 8 Do you want more? Y/N: y ---------------------------------Circle of radius R equation: (x - a)^2 + (y - b)^2 = R^2 Enter center C(a, b) of circle: a = 0 b = 0 Enter radius of circle R = 1 Equation become: (x - 0.00)^2 + (y - 0.00)^2 = 1.00 1: Blue 2: Green 3: Red 4: Cyan 5: Magenta 6: Yellow 7: Black 8: Grey

Enter color(number follow above): 1 Do you want more? Y/N: y ---------------------------------Circle of radius R equation: (x - a)^2 + (y - b)^2 = R^2 Enter center C(a, b) of circle: a = 1 b = 1 Enter radius of circle R = 0.5 Equation become: (x - 1.00)^2 + (y - 1.00)^2 = 0.25 1: Blue 2: Green 3: Red 4: Cyan 5: Magenta 6: Yellow 7: Black 8: Grey Enter color(number follow above): 3 Do you want more? Y/N: n Done! Thank you for using my program

Conclusion: By using Matlab, I created Matlab-Scripts or m-files to solve some equations, equations system. I knew ways to represent functions, use functions and connect those functions. In creation of m-file, I found and used many commands to create file such aswhile, ifelse, fprintf, disp, sqrt, abs I knew the ways so that I can find and directly use commands in library of Matlab to solve equation, equations system. I can represent vector, matrix of m n size and perform many operations on matrix and vector with many commands in packet or library of Matlab. For examples of operations like creating row matrix, column matrix, matrix of size m n, random matrix, and performing operations as *, /, +, inversing, transposing, and finding required elements such as max or min elements of matrix, vector. I can represent and plot some functions and work on graph with some technique such as create title and name of axis, fill color, make comment windows and notes

You might also like