about summary refs log tree commit diff
path: root/usth/MATH2.4/hw
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2020-02-16 14:26:55 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2020-02-16 14:26:55 +0700
commit82e6cf7d1046d6cee16f7e8b044ec33e7ec6c4b7 (patch)
treef7b7ae0bce69070c47a1b31a85bd2bc69dfecf09 /usth/MATH2.4/hw
parent29d1001e2e21eff289bff23412e284c8b2e44595 (diff)
downloadcp-82e6cf7d1046d6cee16f7e8b044ec33e7ec6c4b7.tar.gz
[usth] Numerical Method is MATH2.4
Diffstat (limited to 'usth/MATH2.4/hw')
-rw-r--r--usth/MATH2.4/hw/README.md22
-rwxr-xr-xusth/MATH2.4/hw/cubic25
-rw-r--r--usth/MATH2.4/hw/fibonacci.m7
-rw-r--r--usth/MATH2.4/hw/homework.pdfbin0 -> 122465 bytes
-rwxr-xr-xusth/MATH2.4/hw/linear11
-rwxr-xr-xusth/MATH2.4/hw/quadratic14
6 files changed, 79 insertions, 0 deletions
diff --git a/usth/MATH2.4/hw/README.md b/usth/MATH2.4/hw/README.md
new file mode 100644
index 0000000..fbb5730
--- /dev/null
+++ b/usth/MATH2.4/hw/README.md
@@ -0,0 +1,22 @@
+# Numerical Methods: Homework 1
+
+My name is Nguyễn Gia Phong, my student ID is BI9-184.
+
+The homework were written in Octave and saved as
+
+* `linear`: interactive script to solve linear equation
+* `quadratic`: interactive script to solve quadratic equation
+* `cubic`: interactive script to solve cubic equation
+* `fibonacci.m`: function `fibonacci (n)` returning a vector containing
+  Fibonacci numbers from F<sub>1</sub> to F<sub>n</sub>
+
+The scripts are named without any file extension following the \*nix traditions.
+The homework can be downloaded as a whole in a ZIP package using the button
+near the top-left corner, or alternatively cloned using `git`:
+
+    git clone https://gist.github.com/McSinyx/1d9ff91d1ecfd60254c188bddb7926d5
+
+If this causes any inconvenience, please let me know.
+
+[![Creative Commons License](https://i.creativecommons.org/l/by-sa/4.0/80x15.png)](http://creativecommons.org/licenses/by-sa/4.0/)
+This work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/).
diff --git a/usth/MATH2.4/hw/cubic b/usth/MATH2.4/hw/cubic
new file mode 100755
index 0000000..ca24739
--- /dev/null
+++ b/usth/MATH2.4/hw/cubic
@@ -0,0 +1,25 @@
+#!/usr/bin/env octave
+disp ("Solve quadratic equation a*x^3 + b*x^2 + c*x + d = 0");
+a = input ("a = ");
+while (a == 0)
+  disp ("a must be nonzero");
+  a = input ("a = ");
+end
+b = input ("b = ");
+c = input ("c = ");
+d = input ("d = ");
+
+% Using the trigonometric and hyperbolic solutions on Wikipedia:
+% https://en.wikipedia.org/wiki/Cubic_function
+p = (3*a*c - b^2) / (3*a^2);
+q = (2*b^3 - 9*a*b*c + 27*a^2*d) / (27*a^3);
+if (p == 0)
+  disp ("The given cubic equation has only one distinct solution:");
+  x = cbrt(-q) - b/a/3
+else
+  r = 2 * sqrt(-p/3);
+  disp ("The three solutions of the given quadratic equation are:");
+  for k = 0 : 2
+    x = r * cos((acos(3*q/p/r) - 2*pi*k) / 3) - b/a/3
+  end
+end
diff --git a/usth/MATH2.4/hw/fibonacci.m b/usth/MATH2.4/hw/fibonacci.m
new file mode 100644
index 0000000..340c108
--- /dev/null
+++ b/usth/MATH2.4/hw/fibonacci.m
@@ -0,0 +1,7 @@
+function fib = fibonacci (n)
+  fib = 0 : n;
+  for i = 2 : n
+    fib(i + 1) = fib(i) + fib(i - 1);
+  end
+  fib = fib(2:end);
+end
diff --git a/usth/MATH2.4/hw/homework.pdf b/usth/MATH2.4/hw/homework.pdf
new file mode 100644
index 0000000..9d3f2e9
--- /dev/null
+++ b/usth/MATH2.4/hw/homework.pdf
Binary files differdiff --git a/usth/MATH2.4/hw/linear b/usth/MATH2.4/hw/linear
new file mode 100755
index 0000000..fad7e67
--- /dev/null
+++ b/usth/MATH2.4/hw/linear
@@ -0,0 +1,11 @@
+#!/usr/bin/env octave
+disp ("Solve quadratic equation ax + b = 0");
+a = input ("a = ");
+while (a == 0)
+  disp ("a must be nonzero");
+  a = input ("a = ");
+end
+b = input ("b = ");
+
+disp ("The two solutions of the given linear equation is:");
+x = -b / a
diff --git a/usth/MATH2.4/hw/quadratic b/usth/MATH2.4/hw/quadratic
new file mode 100755
index 0000000..b6ea91a
--- /dev/null
+++ b/usth/MATH2.4/hw/quadratic
@@ -0,0 +1,14 @@
+#!/usr/bin/env octave
+disp ("Solve quadratic equation a*x^2 + b*x + c = 0");
+a = input ("a = ");
+while (a == 0)
+  disp ("a must be nonzero");
+  a = input ("a = ");
+end
+b = input ("b = ");
+c = input ("c = ");
+
+sd = sqrt(b*b - a*c*4);
+disp ("The two solutions of the given quadratic equation are:");
+x0 = (-b - sd) / a / 2
+x1 = (-b + sd) / a / 2