Skip to content
Snippets Groups Projects
Commit e9e7f5b5 authored by Magnus Carlsen Haaland's avatar Magnus Carlsen Haaland
Browse files

Merge branch 'magnch/lf1' into 'master'

Added LF for oving 1

See merge request itgk_hovedgruppe/itgk2023/tdt4110/tdt4110-ovinger!7
parents 56977181 c404e70d
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:d8e68450 tags:
a)
%% Cell type:code id:a27abec5 tags:
``` python
def a_pluss_b_ganger_a_minus_b(a, b):
return (a + b) * (a - b)
def a_opphoyd_i_annen_minus_b_opphoyd_i_annen(a, b):
return (a ** 2 - b**2)
def avvik(a, b):
return a_pluss_b_ganger_a_minus_b(a, b) - a_opphoyd_i_annen_minus_b_opphoyd_i_annen(a, b)
```
%% Cell type:markdown id:45cb676c tags:
b)
%% Cell type:code id:474d0f14 tags:
``` python
import numpy as np
def areal1(a, b, c):
s_verdi = s(a, b, c)
return np.sqrt(s_verdi * (s_verdi - a) * (s_verdi - b) * (s_verdi - c))
def areal2(a, b, c):
return np.sqrt(((a + (b + c)) * (c - (a - b)) * (c + (a -b)) * (a + (b - c))))/4
```
%% Cell type:markdown id:30801ef4 tags:
b)
%% Cell type:code id:ddd558ba tags:
``` python
import numpy as np
import math
T = 3.1e11 # et stort tall
t = 3.1e-5 # et lite tall
# Liste med stort tall fremst, bruker Pythons standard sum-funksjon
L = [T,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t,t]
print(f'Standard sum(): {sum(L)}')
# Numpy array, stort tall fremst, np.sum() funksjon
A = np.array(L)
print(f'Numpy sum(): {np.sum(A)}')
# math.fsum() funksjonen
print(f'Math fsum(): {math.fsum(L)}')
```
%% Output
Standard sum(): 310000000000.0024
Numpy sum(): 310000000000.00134
Math fsum(): 310000000000.0012
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
import numpy as np
def omkrets_sirkel(r):
return 2 * np.pi * r
def areal_sirkel(r):
return np.pi * r**2
def areal_sylinder(r, h):
return 2 * areal_sirkel(r) + 2 * np.pi * r * h
def volum_sylinder(r, h):
return areal_sirkel(r) * h
```
%% Cell type:markdown id: tags:
b)
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
r = np.linspace(0, 2, 6)
h = 3
plt.plot(r, areal_sirkel(r))
plt.plot(r, areal_sylinder(r, h))
plt.show()
```
%% Output
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
navn = 'Ada'
favorittfag = 'ITGK'
print(f'Hei, {navn}')
print(f'{favorittfag} - interessant!')
print(f'Ha en fin dag, {navn}')
print(f'- og lykke til med {favorittfag}')
```
%% Output
Hei, Ada
ITGK - interessant!
Ha en fin dag, Ada
- og lykke til med ITGK
%% Cell type:markdown id: tags:
b)
%% Cell type:code id: tags:
``` python
import math
import numpy as np
r = 5.4
h = 7.9
print("Har en sirkel med radius", r, "som er grunnflate i en sylinder med høyde", h)
omkrets = math.tau * r
print("Omkrets av sirkelen:", omkrets) #tau er det samme som 2 pi
areal_sirkel = np.pi * r**2
print("Areal av sirkelen:", areal_sirkel)
areal_sylinder = omkrets * h + 2 * areal_sirkel
print("Areal av sylinderen:", areal_sylinder)
```
%% Output
Har en sirkel med radius 5.4 som er grunnflate i en sylinder med høyde 7.9
Omkrets av sirkelen: 33.929200658769766
Areal av sirkelen: 91.60884177867838
Areal av sylinderen: 451.25836876163794
%% Cell type:markdown id: tags:
c)
%% Cell type:code id: tags:
``` python
fornavn = "Per"
ideal_alder = 42
kundensAlder = 37
differanse = ideal_alder - kundensAlder
print(fornavn, "er", differanse, "år unna idealalderen")
```
%% Output
Per er 5 år unna idealalderen
%% Cell type:markdown id: tags:
d)
%% Cell type:code id: tags:
``` python
navn = 'Bob Bernt'
alder = 46
print(f'Jeg heter {navn}, og er {alder} år')
```
%% Output
Jeg heter Bob Bernt, og er 46 år
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
a = 2
b = 3
c = 5*a + b
d = a * b + c
e = (-b + 4) / (a - 4)
f = 5 ** (a * b + 2)
g = (a + b) * c - d
```
%% Cell type:markdown id: tags:
b)
%% Cell type:code id: tags:
``` python
import numpy as np
def f(x):
return 2 * x + 1
def g(x):
return (-4 * x + 2) / (5 * x + 3)
def h(x):
return x**2 + 2*x + 1
def i(x):
return np.sqrt(x)
def j(x):
return np.sin(x) + np.cos(x)
```
%% Cell type:markdown id: tags:
c)
%% Cell type:code id: tags:
``` python
def antall_minutt_sekund(minutter):
return minutter // 60
def antall_dogn_timer(timer):
return timer // 24
def antall_timer_minutt_sekund(sek):
timer = sek // 3600
minutter = sek % 60
return timer, minutter
```
This diff is collapsed.
%% Cell type:markdown id:dd5b8b22 tags:
a)
%% Cell type:code id:e5b575cd tags:
``` python
def pris_m_valgfri_moms(pris_u_moms, moms):
return pris_u_moms * (1+ moms)
def pris_u_moms(pris):
return pris / 1.25
```
%% Cell type:markdown id:232a8da1 tags:
b)
%% Cell type:code id:7ceeaa17 tags:
``` python
import numpy as np
priser_inkl_moms = np.array([25, 18, 42, 75, 5, 34.5])
moms = pris_m_valgfri_moms(priser_inkl_moms, .1)
moms_til_betaling = np.sum(moms)
moms_til_betaling
```
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
def legg_sammen_to_tall(a, b):
return int(a) + int(b)
print(legg_sammen_to_tall(10, 15))
```
%% Output
25
%% Cell type:markdown id: tags:
b)
%% Cell type:code id: tags:
``` python
def legg_til_landskode(telefonnummer, landskode):
return f'+{landskode} {telefonnummer}'
```
%% Cell type:markdown id: tags:
c)
%% Cell type:code id: tags:
``` python
a = '1'
b = True
c = False
d = '1.5'
e = '2.45'
a = int(a)
b = int(b)
c = int(c)
d = int(float(d))
e = int(float(e))
```
%% Cell type:markdown id: tags:
d)
%% Cell type:code id: tags:
``` python
import numpy as np
def mult_list_with_x(l, x):
return list(np.array(l) * x)
```
%% Cell type:markdown id: tags:
e)
%% Cell type:code id: tags:
``` python
def rund_av(tall, desimaler):
return round(tall, desimaler)
```
%% Cell type:markdown id: tags:
f)
%% Cell type:code id: tags:
``` python
def negativt_eller_positivt(tall):
return 2 * bool(tall + abs(tall)) - bool(tall)
```
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
import numpy as np
def areal(h):
a = (3 / np.sqrt(6)) * h
return np.sqrt(3) * np.power(a, 2)
```
%% Cell type:markdown id: tags:
b)
%% Cell type:code id: tags:
``` python
import numpy as np
def volum(h):
h = 3
a = 3/np.sqrt(6)*h
return 1/12*np.sqrt(2)*np.power(a,3)
```
%% Cell type:markdown id: tags:
c)
%% Cell type:code id: tags:
``` python
import numpy as np
def areal(h):
a = calculate_a(h)
return np.sqrt(3) * np.power(a, 2)
def volum(h):
a = calculate_a(h)
return 1/12*np.sqrt(2)*np.power(a,3)
def calculate_a(h):
return (3 / np.sqrt(6)) * h
```
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
def E(v):
return 6.626e-34 * v
```
%% Cell type:markdown id: tags:
b)
%% Cell type:code id: tags:
``` python
def molekyler_av_stoff(g, molvekt):
return 6.022e+23 * (g / molvekt)
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment