0% found this document useful (0 votes)
38 views

Math Lab Assignment

The document contains 7 questions and solutions regarding Fortran programming. Question 1 involves creating a loop to calculate values of z for given values of x and y. Question 2 involves using if/else statements to calculate f(x) at intervals of 0.5 from -10 to 10. Question 3 asks to write a program to find the roots of a quadratic equation. Question 4 calculates e^5 and e^1.5 using a Taylor series. Question 5 finds the root of an equation using bisection method. Question 6 evaluates an integral using Simpson's rule. Question 7 uses Lagrange interpolation to evaluate y for a given x value.

Uploaded by

somapaul1323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Math Lab Assignment

The document contains 7 questions and solutions regarding Fortran programming. Question 1 involves creating a loop to calculate values of z for given values of x and y. Question 2 involves using if/else statements to calculate f(x) at intervals of 0.5 from -10 to 10. Question 3 asks to write a program to find the roots of a quadratic equation. Question 4 calculates e^5 and e^1.5 using a Taylor series. Question 5 finds the root of an equation using bisection method. Question 6 evaluates an integral using Simpson's rule. Question 7 uses Lagrange interpolation to evaluate y for a given x value.

Uploaded by

somapaul1323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

𝑸𝟏: If 𝑧 = 𝑥 2 − 2𝑥𝑦 + 3𝑦 2 − 8𝑥 + 3𝑦 − 8, find the value of 𝑧 for the values of 𝑥, 𝑦

in [−3,3] with increment 0.2.

Solution:
𝑃𝑟𝑖𝑛𝑡 ∗, " 𝑋 𝑌 𝑍"
𝑥 = −3.0
𝑑𝑜 𝑖 = 1,31
𝑦 = −3.0
𝑑𝑜 𝑗 = 1,31
𝑧 = 𝑥 ∗∗ 2 − 2 ∗ 𝑥 ∗ 𝑦 + 3 ∗ 𝑦 ∗∗ 2 − 8 ∗ 𝑥 + 3 ∗ 𝑦 − 8
𝑝𝑟𝑖𝑛𝑡 ∗, 𝑥, 𝑦, 𝑧
𝑦 = 𝑦 + 0.2
𝑒𝑛𝑑 𝑑𝑜
𝑥 = 𝑥 + 0.2
𝑒𝑛𝑑 𝑑𝑜
𝑠𝑡𝑜𝑝
𝑒𝑛𝑑

1 − 𝑥 2 𝑖𝑓 𝑥 < 0
𝑸𝟐: If 𝑓(𝑥) = {1 + 2𝑥 𝑖𝑓 0 ≤ 𝑥 < 1 if 𝑥 = [−10,10] then find the value of
1
3 + 𝑖𝑓 𝑥 > 1
𝑥
𝑓(𝑥) at 0.5 steps.
Solution:
𝑝𝑟𝑖𝑛𝑡 ∗, " 𝑥 𝑦"
𝑥 = −10
𝑑𝑜 𝑖 = 1,41
𝑖𝑓(𝑥. 𝐿𝑇. 0)𝑓 = 1 − 𝑥 ∗∗ 2
𝑖𝑓((𝑥. 𝐺𝐸. 0). 𝐴𝑁𝐷. (𝑥. 𝐿𝑇. 1))𝑓 = 1 + 2 ∗ 𝑥
𝑖𝑓(𝑥. 𝐺𝑇. 1)𝑓 = 3 + 1/𝑥
𝑃𝑟𝑖𝑛𝑡 ∗, 𝑥, 𝑓
𝑥 = 𝑥 + 0.5
𝑒𝑛𝑑 𝑑𝑜
𝑠𝑡𝑜𝑝
𝑒𝑛𝑑
𝑸𝟑: Write Fortran program that reads 𝑎, 𝑏, 𝑐 and find the value of 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 =
0.
Solution:
𝑃𝑟𝑖𝑛𝑡 ∗, ′𝐸𝑛𝑡𝑒𝑟 𝑎, 𝑏, 𝑐′
𝑅𝑒𝑎𝑑 ∗, 𝑎, 𝑏, 𝑐
𝑑 = 𝑏 ∗∗ 2 − 4 ∗ 𝑎 ∗ 𝑐
𝑝 = −𝑏/(2 ∗ 𝑎)
𝑞 = 𝑆𝑞𝑟𝑡(𝑎𝑏𝑠(𝑑))/(2 ∗ 𝑎)
𝐼𝑓 (𝑑. 𝑔𝑒. 0)𝑡ℎ𝑒𝑛
𝑃𝑟𝑖𝑛𝑡 ∗, ′1𝑠𝑡 𝑟𝑜𝑜𝑡 = ′, 𝑝 + 𝑞
𝑃𝑟𝑖𝑛𝑡 ∗, ′2𝑛𝑑 𝑟𝑜𝑜𝑡 = ′, 𝑝 − 𝑞
𝑒𝑙𝑠𝑒
𝑃𝑟𝑖𝑛𝑡 ∗, ′1𝑠𝑡 𝑟𝑜𝑜𝑡 = ′, 𝑃, ′ + 𝑖′, 𝑞
𝑃𝑟𝑖𝑛𝑡 ∗, ′2𝑛𝑑 𝑟𝑜𝑜𝑡 = ′, 𝑃, ′ − 𝑖′, 𝑞
𝐸𝑛𝑑 𝐼𝑓
𝑠𝑡𝑜𝑝
𝑒𝑛𝑑

𝑸𝟒: Write a Fortran program to compute the value of 𝑒 5 and 𝑒 1.5 with the help of
five terms of the series 𝑒 𝑥 .

Solution:
𝐼𝑛𝑡𝑒𝑔𝑒𝑟 𝑓𝑎𝑐𝑡
𝑒(𝑥) = 1 + 𝑥 + 𝑥 ∗∗ 2/𝑓𝑎𝑐𝑡(2) + 𝑥 ∗∗ 3/𝑓𝑎𝑐𝑡(3) + 𝑥 ∗∗ 4/𝑓𝑎𝑐𝑡(4)
𝑝𝑟𝑖𝑛𝑡 ∗, "𝑒^5 = ", 𝑒(5.0)
𝑝𝑟𝑖𝑛𝑡 ∗, "𝑒^1.5 = ", 𝑒(1.5)
𝑒𝑛𝑑
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑓𝑎𝑐𝑡(𝑛)
𝑖𝑛𝑡𝑒𝑔𝑒𝑟 𝑓𝑎𝑐𝑡
𝑓𝑎𝑐𝑡 = 1
𝑑𝑜 𝑖 = 1, 𝑛
𝑓𝑎𝑐𝑡 = 𝑓𝑎𝑐𝑡 ∗ 𝑖
𝑒𝑛𝑑𝑑𝑜
𝑟𝑒𝑡𝑢𝑟𝑛
𝑒𝑛𝑑
𝑸𝟓: Write a Fortran program to find the root of the equation 𝑥 2 − 𝑥 − 1 = 0 by
bisection method.
Solution:
𝑓(𝑥) = 𝑥 ∗∗ 2 − 𝑥 − 1
50 𝑝𝑟𝑖𝑛𝑡
∗, ′𝑒𝑛𝑡𝑒𝑟 𝑡ℎ𝑒 𝑡𝑟𝑖𝑎𝑙 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛𝑠, 𝑡𝑜𝑙𝑒𝑟𝑎𝑛𝑐𝑒 𝑎𝑛𝑑 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑖𝑡𝑒𝑟𝑎𝑡𝑖𝑜𝑛′
𝑟𝑒𝑎𝑑 ∗, 𝑎, 𝑏, 𝑡𝑜𝑙, 𝑛
𝑖𝑓(𝑓(𝑎) ∗ 𝑓(𝑏). 𝑔𝑒. 0) 𝑔𝑜 𝑡𝑜 30
𝑝𝑟𝑖𝑛𝑡 ∗, ′ 𝑛 𝑎 𝑏 𝑝 ′
𝑑𝑜 𝑖 = 1, 𝑛
𝑝 = (𝑎 + 𝑏)/2
𝑝𝑟𝑖𝑛𝑡 ∗, 𝑖, 𝑎, 𝑏, 𝑝
𝑖𝑓(𝑎𝑏𝑠(𝑝 − 𝑎). 𝐿𝑇. 𝑡𝑜𝑙) 𝑔𝑜 𝑡𝑜 20
𝑖𝑓(𝑓(𝑎) ∗ 𝑓(𝑝). 𝑔𝑒. 0) 𝑡ℎ𝑒𝑛
𝑎=𝑝
𝑒𝑙𝑠𝑒
𝑏=𝑝
𝑒𝑛𝑑 𝑖𝑓
𝑒𝑛𝑑 𝑑𝑜
30 𝑝𝑟𝑖𝑛𝑡 ∗, ′ 𝑇ℎ𝑒 𝑚𝑒𝑡ℎ𝑜𝑑 𝑓𝑎𝑖𝑙𝑠 ′
𝑔𝑜 𝑡𝑜 50
20 𝑃𝑟𝑖𝑛𝑡 ∗, ′𝑇ℎ𝑒 𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 𝑖𝑠 = ′, 𝑝
𝑠𝑡𝑜𝑝
𝑒𝑛𝑑
2 1 1
𝑸𝟔: Write a FORTRAN code to evaluate the integral ∫1 𝑑𝑥 by simpson’s
1+𝑥 2 3
rule.
Solution:
𝑓(𝑥) = 1/(1 + 𝑥 ∗∗ 2)
𝑝𝑟𝑖𝑛𝑡 ∗, ′𝐸𝑛𝑡𝑒𝑟 𝑡ℎ𝑒 𝑙𝑜𝑤𝑒𝑟 𝑙𝑖𝑚𝑖𝑡 , 𝑢𝑝𝑝𝑒𝑟 𝑙𝑖𝑚𝑖𝑡, 𝑤𝑖𝑑𝑡ℎ: ′
𝑟𝑒𝑎𝑑(∗,∗)𝑎, 𝑏, 𝑛
ℎ = (𝑏 − 𝑎)/𝑛
𝑠𝑢𝑚1 = (𝑓(𝑎) + 𝑓(𝑏))
𝑠𝑢𝑚2 = 0.0
𝑠𝑢𝑚3 = 0.0
𝑑𝑜 𝑖 = 1, 𝑛 − 1
𝑖𝑓(𝑚𝑜𝑑(𝑖, 2). 𝑒𝑞. 0.0)𝑡ℎ𝑒𝑛
𝑠𝑢𝑚3 = 𝑠𝑢𝑚3 + 𝑓(𝑎 + 𝑖 ∗ ℎ)
𝑒𝑙𝑠𝑒
𝑠𝑢𝑚2 = 𝑠𝑢𝑚2 + 𝑓(𝑎 + 𝑖 ∗ ℎ)
𝑒𝑛𝑑 𝑖𝑓
𝑒𝑛𝑑 𝑑𝑜
𝑎𝑟𝑒𝑎 = (ℎ/3.0) ∗ (𝑠𝑢𝑚1 + 4.0 ∗ 𝑠𝑢𝑚2 + 2.0 ∗ 𝑠𝑢𝑚3)
𝑃𝑟𝑖𝑛𝑡 ∗, ′𝑇ℎ𝑒 𝑎𝑟𝑒𝑎 𝑖𝑠 = ′, 𝑎𝑟𝑒𝑎
𝑠𝑡𝑜𝑝
𝑒𝑛𝑑
𝑸𝟕: Use Lagrange’s interpolation formula to evaluate the value of 𝑦 at 𝑥 = 4.6 by
for the following data.
X 1 3 4 4.5 5 7 9
Y 1 9 16 20.25 25 49 81

Solution:
𝑟𝑒𝑎𝑙 𝑥(10), 𝑦(10)
𝑝𝑟𝑖𝑛𝑡 ∗, ′𝑒𝑛𝑡𝑒𝑟 𝑡ℎ𝑒 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑝𝑜𝑖𝑛𝑡𝑠′
𝑟𝑒𝑎𝑑 ∗, 𝑛
𝑝𝑟𝑖𝑛𝑡 ∗, ′𝐸𝑛𝑡𝑒𝑟 𝑡ℎ𝑒 𝑣𝑎𝑙𝑢𝑒𝑠 𝑜𝑓 𝑥 𝑎𝑛𝑑 𝑦′
𝑟𝑒𝑎𝑑 ∗, (𝑥(𝑖), 𝑖 = 1, 𝑛), (𝑦(𝑖), 𝑖 = 1, 𝑛)
𝑝𝑟𝑖𝑛𝑡 ∗, ′𝑒𝑛𝑡𝑒𝑟 𝑥 𝑡𝑜 𝑐𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑒 𝑦′
𝑟𝑒𝑎𝑑 ∗, 𝑥0
𝑦𝑝 = 0.0
𝑑𝑜 𝑗 = 1, 𝑛
𝑦𝑑 = 1.0
𝑑𝑜 𝑖 = 1, 𝑛
𝑖𝑓(𝑖. 𝑒𝑞. 𝑗)𝑔𝑜 𝑡𝑜 7
𝑦𝑑 = 𝑦𝑑 ∗ ((𝑥0 − 𝑥(𝑖))/(𝑥(𝑗) − 𝑥(𝑖)))
7 𝑒𝑛𝑑𝑑𝑜
𝑦𝑝 = 𝑦𝑝 + 𝑦𝑑 ∗ 𝑦(𝑗)
𝑒𝑛𝑑 𝑑𝑜
𝑤𝑟𝑖𝑡𝑒(6,12)𝑦𝑝
12 𝑓𝑜𝑟𝑚𝑎𝑡(′𝑇ℎ𝑒 𝑣𝑎𝑙𝑢𝑒 𝑖𝑠 = ′, 𝑓10.4)
𝑠𝑡𝑜𝑝
𝑒𝑛𝑑

𝑸𝟖: Every year 12000 tk is deposited in a saving account in 1567 which yield 5%
interest compounded annually. Write a Fortran program which prints account
number and the amount in the account after 10 years.

Solution:
𝑝 = 12000
𝐴𝑐 = 1567
𝑝𝑟𝑖𝑛𝑡 ∗, ′𝐴𝑐𝑐𝑜𝑢𝑛𝑡 𝑁𝑜 = ′, 𝐴𝑐
𝑖=1
20 𝑝 = 𝑝 ∗ (1 + 0.05) + 12000
𝑖 =𝑖+1
𝑖𝑓(𝑖. 𝐿𝑒. 10)𝑔𝑜𝑡𝑜 20
𝑝𝑟𝑖𝑛𝑡 ∗, ′𝑇𝑜𝑡𝑎𝑙 𝑎𝑚𝑜𝑢𝑛𝑡 = ′, 𝑝
𝑠𝑡𝑜𝑝
𝑒𝑛𝑑

You might also like