From e461df7573c2b7b7e26c965d8cf2d8e175d67378 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Mon, 16 Dec 2019 21:31:18 +0700 Subject: [usth/MATH2.2] Numerical Methods The future starts now. --- usth/MATH2.2/final/EX2.m | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 usth/MATH2.2/final/EX2.m (limited to 'usth/MATH2.2/final/EX2.m') diff --git a/usth/MATH2.2/final/EX2.m b/usth/MATH2.2/final/EX2.m new file mode 100644 index 0000000..9b8c95e --- /dev/null +++ b/usth/MATH2.2/final/EX2.m @@ -0,0 +1,38 @@ +disp ("Question 2:"); +disp ("(a)"); +pkg load symbolic; +syms x real; +solve (sqrt (x) - x + 1 == 0) +% ans = (sym) +% √5 3 +% ── + ─ +% 2 2 +pkg unload symbolic; +disp ("To get numerical solutions we can use fzero"); +disp ("With the initial guess of 0, fzero (@(x) sqrt (x) - x + 1, 0) returns"); +fzero (@(x) sqrt (x) - x + 1, 0) + +disp ("(b)"); +hold on; +ezplot (@(x) exp (-x)); +ezplot (@(x) sin (x)); +hold off; +disp ("Press any key to continue..."); +kbhit; + +disp ("(c)"); +s = 0; +for k = 1 : 1000 + s += k^3; +endfor +printf ("The cubic sum of integers from 1 to 1000 is %d\n", s); + +disp ("(d)"); +A = [2 1 4 + 1 2 -5 + 3 -2 4]; +b = [10 1 8]'; +disp ("Using mldivide, [x y z] = "); +disp (mldivide (A, b)'); +disp ("Using inv, [x y z] = "); +disp ((inv (A) * b)'); -- cgit 1.4.1