- 1st Aug 2024
- 18:19 pm
Program for Root Finding Problem: As the title suggests, the Root-Finding Problem is the problem of finding a root of the equation f(x) = 0, where f(x) is a function of a single variable x. Write a program in the Python for the following methods to solve the problem of numerical approximation, the root-finding problem within 10−5 for the given problem. Methods:
- Bisection method
- Fixed Point method (use g(x)=f(x)+x)
- Newton’s method (d) Secant’s method Scenario:
- Program should ask the user to enter the equation
- Program will ask to enter the interval to find root.
- Program should ask the user to enter the method to be used
- Program should ask the stopping criterion
- If stopping criterion is missing, Program should take a by default stopping criterion of root-finding within 10−5 for the given problem and should display.
- Program will display the roots of the given equation in case the equation has a root inside the given interval. In case, there is no root in the given interval show a message.
IN YOUR REPORT
Write your name, ID, section, department, and submission date along with the title “Project on Computational Numerical Analysis (CS401)” in the front page. Then (a) Give the flowchart of your Program, and for each algorithm.
- Write the pseudo code of the Program, and for each algorithm.
- Complete the following table until the stopping condition is satisfied. (d) Compare your results and write your comments.
Part (2) - Principal Component Analysis (PCA)
PCA is a commonly used dimensionality reduction method. It works by computing the principal components and performing a change of basis. It retains the data in the direction of maximum variance.
Datasets: IRIS, MNIST, or any other image dataset you are familiar with.
- Implement PCA step by step (don’t use ready libraries).
- Select number of optimal features automatically.
- Visualize extracted features.
- Reconstruct the original images using extracted features.
- Classify the features using any classifier like SVM or Random forest.
Free Assignment Solution - Computational Numerical Analysis - CS401
{
"cells": [
{
"cell_type": "markdown",
"id": "b6b15c14",
"metadata": {},
"source": [
"# Part 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "975943a6",
"metadata": {},
"outputs": [],
"source": [
"function = input('enter your equation (like a*x^d + b*x^f + c) coefficents with comma seperated : ').split(',')\n",
"def func(x):\n",
" return float(function[0]) * x ** float(function[1]) + float(function[2]) * x ** float(function[3]) + float(function[4])\n",
"\n",
"a, b = list(map(float, input('enter the enterval with comma seperated: ').split(',')))\n",
"\n",
"epsilon = input('enter stopping criterion epsilon : ')\n",
"\n",
"if len(epsilon) == 0:\n",
" epsilon = 0.00001\n",
"else:\n",
" epsilon = float(epsilon)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a7461c45",
"metadata": {},
"outputs": [],
"source": [
"print('bisection --- 1\\n\\nfixed point iteration --- 2\\n\\nnewton --- 3\\n\\nsecant --- 4')\n",
"method = int(input('enter a method number you want to use : '))\n",
"\n",
"\n",
"if method == 1 :\n",
" def bisection(a,b):\n",
"\n",
" if (func(a) * func(b) >= 0):\n",
" print(\"there is not root between a and b\\n\")\n",
" return\n",
"\n",
" global lisc\n",
" lisc = []\n",
"\n",
" c = a\n",
" while ((b-a) >= 0.01):\n",
"\n",
" # Find middle point\n",
" c = (a+b)/2\n",
"\n",
" # Check if middle point is root\n",
" if (func(c) == 0.0):\n",
" break\n",
"\n",
" # Decide the side to repeat the steps\n",
" if (func(c)*func(a) < 0):\n",
" b = c\n",
" else:\n",
" a = c\n",
" lisc.append(c)\n",
" print(\"The value of root is : \",\"%.5f\"%c)\n",
"\n",
" # Driver code\n",
" # Initial values assumed\n",
" \n",
" bisection(a, b)\n",
" plt.plot(lisc)\n",
" plt.xlabel('x')\n",
" plt.ylabel('y')\n",
" plt.show()\n",
"\n",
"elif method == 2:\n",
" import math\n",
"\n",
" def func(x):\n",
" return float(function[0]) * x ** float(function[1]) + float(function[2]) * x ** float(function[3]) + float(function[4])\n",
"\n",
" # Re-writing f(x)=0 to x = g(x)\n",
" def g(x):\n",
" return 1/math.sqrt(1+x)\n",
"\n",
" # Implementing Fixed Point Iteration Method\n",
" def fixedPointIteration(x0, e, N):\n",
" print('\\n\\n*** FIXED POINT ITERATION ***')\n",
" step = 1\n",
" flag = 1\n",
" condition = True\n",
" \n",
" global lis\n",
" lis = []\n",
" while condition:\n",
" x1 = g(x0)\n",
" print('Iteration-%d, x1 = %0.5f and f(x1) = %0.5f' % (step, x1, func(x1)))\n",
" lis.append(func(x1))\n",
" x0 = x1\n",
"\n",
" step = step + 1\n",
"\n",
" if step > N:\n",
" flag=0\n",
" break\n",
"\n",
" condition = abs(f(x1)) > e\n",
"\n",
" if flag==1:\n",
" print('\\nRequired root is: %0.5f' % x1)\n",
" else:\n",
" print('\\nNot Convergent.')\n",
"\n",
" x0 = float(input('Enter Guess: '))\n",
" e = epsilon\n",
" N = int(input('Maximum Step: '))\n",
"\n",
" # Starting Newton Raphson Method\n",
" fixedPointIteration(x0,e,N)\n",
" plt.plot(lis)\n",
" plt.xlabel('x')\n",
" plt.ylabel('y')\n",
" plt.show()\n",
" \n",
"elif method == 3:\n",
" def derivFunc( x ):\n",
" return 3 * x * x - 2 * x\n",
"\n",
" # Function to find the root\n",
" def newtonRaphson( x ):\n",
" global lis\n",
" lis = []\n",
" h = func(x) / derivFunc(x)\n",
" while abs(h) >= 0.0001:\n",
" h = func(x)/derivFunc(x)\n",
"\n",
" x = x - h\n",
" lis.append(x)\n",
"\n",
" print(\"The value of the root is : \",\"%.4f\"% x)\n",
"\n",
"\n",
" # Driver program to test above\n",
" x0 = -20\n",
" newtonRaphson(x0)\n",
" plt.plot(lis)\n",
" plt.xlabel('x')\n",
" plt.ylabel('y')\n",
" plt.show()\n",
"\n",
"else:\n",
" def secant(x1, x2, E):\n",
" n = 0\n",
" xm = 0 \n",
" x0 = 0 \n",
" c = 0\n",
" global lis\n",
" lis = []\n",
" if (func(x1) * func(x2) < 0):\n",
" while True:\n",
"\n",
" # calculate the intermediate value\n",
" x0 = ((x1 * func(x2) - x2 * func(x1)) / (func(x2) - func(x1)))\n",
"\n",
" # check if x0 is root of\n",
" # equation or not\n",
" c = func(x1) * func(x0)\n",
"\n",
" # update the value of interval\n",
" x1 = x2\n",
" x2 = x0\n",
"\n",
" # update number of iteration\n",
" n += 1\n",
"\n",
" # if x0 is the root of equation\n",
" # then break the loop\n",
" if (c == 0):\n",
" break\n",
" xm = ((x1 * func(x2) - x2 * func(x1)) / (func(x2) - func(x1)))\n",
"\n",
" if(abs(xm - x0) < E):\n",
" break\n",
" \n",
" lis.append(x0)\n",
"\n",
" print(\"Root of the given equation =\", round(x0, 6))\n",
" print(\"No. of iterations = \", n)\n",
"\n",
" else:\n",
" print(\"Can not find a root in \", \"the given interval\")\n",
"\n",
"\n",
"\n",
" x1 = a\n",
" x2 = b\n",
" E = epsilon\n",
" secant(x1, x2, E)\n",
" plt.plot(lis)\n",
" plt.xlabel('x')\n",
" plt.ylabel('y')\n",
" plt.show()\n",
"\n",
"print(20 * '-', 'End', 20 * '-')"
]
},
{
"cell_type": "markdown",
"id": "9e63635e",
"metadata": {},
"source": [
"# Part 2"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e042f249",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.decomposition import PCA\n",
"from sklearn import preprocessing\n",
"import matplotlib.pyplot as plt\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fb8a8dd6",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\"\n",
"\n",
"df = pd.read_csv(url, names=['sepal length', 'sepal width' , 'petal lenght', 'petal width', 'target'])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4cf5e406",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal length</th>\n",
" <th>sepal width</th>\n",
" <th>petal lenght</th>\n",
" <th>petal width</th>\n",
" <th>target</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>3.5</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>3.0</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>3.2</td>\n",
" <td>1.3</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>3.6</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal length sepal width petal lenght petal width target\n",
"0 5.1 3.5 1.4 0.2 Iris-setosa\n",
"1 4.9 3.0 1.4 0.2 Iris-setosa\n",
"2 4.7 3.2 1.3 0.2 Iris-setosa\n",
"3 4.6 3.1 1.5 0.2 Iris-setosa\n",
"4 5.0 3.6 1.4 0.2 Iris-setosa"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4fce6803",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"features = [i for i in df.columns[:-1]]\n",
"\n",
"x = df.loc[:, features].values\n",
"\n",
"y = df.loc[:, ['target']].values\n",
"\n",
"x = StandardScaler().fit_transform(x)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "eab153e0",
"metadata": {},
"outputs": [],
"source": [
"pca = PCA(n_components=2)\n",
"\n",
"principalComponents = pca.fit_transform(x)\n",
"\n",
"principalDataframe = pd.DataFrame(data = principalComponents, columns = ['PC1', 'PC2'])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4489ddb9",
"metadata": {},
"outputs": [],
"source": [
"targetDataframe = df[['target']]\n",
"\n",
"newDataframe = pd.concat([principalDataframe, targetDataframe],axis = 1)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d171a168",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>PC1</th>\n",
" <th>PC2</th>\n",
" <th>target</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>-2.264542</td>\n",
" <td>0.505704</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>-2.086426</td>\n",
" <td>-0.655405</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>-2.367950</td>\n",
" <td>-0.318477</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>-2.304197</td>\n",
" <td>-0.575368</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>-2.388777</td>\n",
" <td>0.674767</td>\n",
" <td>Iris-setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>145</th>\n",
" <td>1.870522</td>\n",
" <td>0.382822</td>\n",
" <td>Iris-virginica</td>\n",
" </tr>\n",
" <tr>\n",
" <th>146</th>\n",
" <td>1.558492</td>\n",
" <td>-0.905314</td>\n",
" <td>Iris-virginica</td>\n",
" </tr>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>1.520845</td>\n",
" <td>0.266795</td>\n",
" <td>Iris-virginica</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>1.376391</td>\n",
" <td>1.016362</td>\n",
" <td>Iris-virginica</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>0.959299</td>\n",
" <td>-0.022284</td>\n",
" <td>Iris-virginica</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>150 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" PC1 PC2 target\n",
"0 -2.264542 0.505704 Iris-setosa\n",
"1 -2.086426 -0.655405 Iris-setosa\n",
"2 -2.367950 -0.318477 Iris-setosa\n",
"3 -2.304197 -0.575368 Iris-setosa\n",
"4 -2.388777 0.674767 Iris-setosa\n",
".. ... ... ...\n",
"145 1.870522 0.382822 Iris-virginica\n",
"146 1.558492 -0.905314 Iris-virginica\n",
"147 1.520845 0.266795 Iris-virginica\n",
"148 1.376391 1.016362 Iris-virginica\n",
"149 0.959299 -0.022284 Iris-virginica\n",
"\n",
"[150 rows x 3 columns]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"newDataframe"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "0d6d3317",
"metadata": {},
"outputs": [
{
"data": {
"image/png":
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"percent_variance = np.round(pca.explained_variance_ratio_* 100, decimals =2)\n",
"columns = ['PC1', 'PC2']\n",
"plt.bar(x= range(1,3), height=percent_variance, tick_label=columns)\n",
"plt.ylabel('Percentate of Variance Explained')\n",
"plt.xlabel('Principal Component')\n",
"plt.title('PCA Scree Plot')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "64b56f52",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(0, 0.5, 'PC2')"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png":
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(principalDataframe.PC1, principalDataframe.PC2)\n",
"plt.title('PC1 against PC2')\n",
"plt.xlabel('PC1')\n",
"plt.ylabel('PC2')"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "52c4a7e7",
"metadata": {},
"outputs": [
{
"data": {
"image/png":
"text/plain": [
"<Figure size 576x576 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"fig = plt.figure(figsize = (8,8))\n",
"ax = fig.add_subplot(1,1,1) \n",
"ax.set_xlabel('PC1')\n",
"ax.set_ylabel('PC2')\n",
"\n",
"ax.set_title('Plot of PC1 vs PC2', fontsize = 20)\n",
"\n",
"targets = ['Iris-setosa', 'Iris-versicolor', 'Iris-virginica']\n",
"\n",
"colors = ['r', 'g', 'b']\n",
"\n",
"for target, color in zip(targets,colors):\n",
" indicesToKeep = newDataframe['target'] == target\n",
" ax.scatter(newDataframe.loc[indicesToKeep, 'PC1']\n",
" , newDataframe.loc[indicesToKeep, 'PC2']\n",
" , c = color\n",
" , s = 50)\n",
" \n",
"ax.legend(targets)\n",
"ax.grid()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "ce3f73d9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.72770452, 0.23030523])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pca.explained_variance_ratio_"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Get the best Computational Numerical Analysis - CS401 assignment help and tutoring services from our experts now!
Our team of Python experts has successfully finished this sample Python assignment. The provided solutions are intended solely for research and reference. If you find the reports and code useful, our Python tutors would be happy to help.
- For a complete solution package that includes code, reports, and screenshots, please check out our Python Assignment Sample Solution page.
- For personalized online tutoring sessions to address any questions you have about this assignment, reach out to our Python experts.
- For additional insights, explore the partial solution available in the blog above.
About The Author - Alex Johnson
Alex Johnson is a Computer Science student specializing in numerical analysis. His project, "Program for Root-Finding Problem," showcases proficiency in Python by implementing Bisection, Fixed Point, Newton’s, and Secant methods to solve root-finding problems. The program features user-friendly input options and provides accurate solutions within a specified tolerance.