Skip to content

Commit f3f4be1

Browse files
committed
quadratic Eq. How to solve
1 parent 84d4fc9 commit f3f4be1

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
1) Lists: Introduction
44
2) Lists: Example, computing Khayyam-Pascal's triangle
5-
3) Lists: Copying
5+
3) Lists: Copying
6+
4) Exercise: Solving quadratic equation

quadratic equation-how to solve.ipynb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Python: Everything\n",
8+
"- 4) Example: Solving Quadratic equation\n",
9+
"<br>----------------------------------------------\n",
10+
"<br> By Hamed Shah-Hosseini\n",
11+
"<br> https://github.com/ostad-ai/Python-Everything"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"Quadratic equation: $ax^{2}+bx+c=0$ <br> is solved by: $x=\\frac{-b\\pm\\sqrt{b^{2}-4ac}}{2a}$"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 8,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"from math import sqrt\n",
28+
"\n",
29+
"def quadratic(x,a,b,c):\n",
30+
" return a*x**2+b*x+c\n",
31+
"\n",
32+
"def solveQ(a,b,c):\n",
33+
" x=[None]*2\n",
34+
" if a==0:\n",
35+
" if b!=0:\n",
36+
" x[0]=x[1]=-c/b\n",
37+
" else:\n",
38+
" delta=b**2-4*a*c\n",
39+
" if delta>=0:\n",
40+
" temp=sqrt(delta)\n",
41+
" x[0]=(-b+temp)/(2*a)\n",
42+
" x[1]=(-b-temp)/(2*a)\n",
43+
" return x"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 13,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"name": "stdout",
53+
"output_type": "stream",
54+
"text": [
55+
"solutions: -3.0 -5.0\n"
56+
]
57+
}
58+
],
59+
"source": [
60+
"a,b,c=[1,8,15]\n",
61+
"x=solveQ(a,b,c)\n",
62+
"if x[0] is None:\n",
63+
" print('No real solution')\n",
64+
"else:\n",
65+
" print('solutions: ',x[0],x[1])"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": []
74+
}
75+
],
76+
"metadata": {
77+
"kernelspec": {
78+
"display_name": "Python 3",
79+
"language": "python",
80+
"name": "python3"
81+
},
82+
"language_info": {
83+
"codemirror_mode": {
84+
"name": "ipython",
85+
"version": 3
86+
},
87+
"file_extension": ".py",
88+
"mimetype": "text/x-python",
89+
"name": "python",
90+
"nbconvert_exporter": "python",
91+
"pygments_lexer": "ipython3",
92+
"version": "3.8.3"
93+
}
94+
},
95+
"nbformat": 4,
96+
"nbformat_minor": 5
97+
}

0 commit comments

Comments
 (0)