wintertreey 님의 블로그
1.4 연습문제, 프로그래밍 실습 본문
1장 연습문제
1. 3번
모든 x는 단 한개의 y값을 가져야한다.
2.
g((1)) = g((2-1)) = f((3*2-5))=f((1))=-2
3.
def f(x):
return x**3-7*x**2+16*x
def g(x):
return 2*x+8
x=np.linspace(-1,6,1001)
g1, = plt.plot(x,f(x))
g2, = plt.plot(x,g(x))
plt.legend(handles=(g1, g2),
labels=(r'$f(x) = x^3 - 7x^2 + 16x$', r'$g(x) = 2x + 8$'))
coeffs = [1, -7, 14, -8]
roots = np.roots(coeffs)
for r in roots:
y= 2*r + 8
#print(f"({r:.6f}, {y:.6f})")
plt.plot(r,y,'ro')
plt.text(r,y,f'({r:.2f}, {y:.2f})')
plt.show()
4.
def f(x):
return np.where(x <= -1, x + 2, x**2)
def g(x):
return np.where(x < 0, 0, 2*x)
x = np.linspace(-3, 3, 601)
g1, = plt.plot(x,f(x))
g2, = plt.plot(x,g(x))
plt.legend(handles=(g1,g2),
labels=(r'$f(x)= x+2(x<=-1), x^2(x>-1)$', r'$g(x)= x+|x|$'))
plt.show()
5.
f((x))= 2((x>=0))
2x+2((x<0))
((a))
y=3((x>=0))
2((x-1))+2+1=2x+1((x<0))
def f(x):
return np.where(x >= 0, 2, 2*x+2)
def g(x):
return np.where(x < 0, 0, 2*x)
x = np.linspace(-3, 3, 601)
plt.plot(x,f(x))
plt.show()
((b))
y=3((x>=0))
-2x+2((x<0))
def f(x):
return np.where(x >= 0, 2, 2*x+2)
def g(x):
return np.where(x < 0, -2*x+2, 3)
x = np.linspace(-3, 3, 601)
# x < 0인 구간만 선택
x1 = x[x < 0]
y1 = g(x1)
# x >= 0인 구간만 선택
x2 = x[x >= 0]
y2 = g(x2)
plt.plot(x1, y1)
plt.plot(x2, y2)
##plt.plot(x,g(x))
plt.show()
6.
a((x-3))+2 = ((ax+2))-3
ax-3a+2 = ax-1
a =1
f((3))=5
7.
((a)) h((x)) = 1/3x^2-1
((b)) h((x)) = 1/3((x-4))^2 +1
8.
((a)) g((-x)) = g((x))
h= f o g = f((g((x))))
f((g((-x)))) = f((g((x))))
True
((b)) f((-x)) = -f((x))
f o f((-x)) = f((f((-x)))) = f((-f(x)))) = -f((f((x)))) = -f o f
True
((c))
짝함수 예시 : y = x^2
홀함수 예시 : y= 2x
def f(x):
return x**2
def g(x):
return 2*x
x = np.linspace(-3, 3, 601)
plt.plot(x, f(x), x, g(x))
plt.show()
9.
단사함수
= 서로 다른 입력 ((x1 =/= x2))에 대해, 항상 서로 다른 출력을 갖는 함수 f((x1)) =/= f((x2))이면 됨.
치역=공역이 아니어도 상관없음.
g o f ((x1)) = g o f ((x2))
g((f((x1)))) = g((f((x2)))))
-> f((x1)) = f((x2))
-> x1 = x2
True
f, g 가 둘 다 단사함수 -> g o f 단사함수
10. 2, 3번
11.
x =1
12.
$f(x) = 3+\sqrt{1+2x}$
def f(x):
return 3 + np.sqrt(1+2*x)
x = np.linspace(-0.5,3,100)
plt.plot(f(x), x)
plt.show()
$g(x) = \frac{1-2x}{1+2x}$
def g(x):
return (1-2*x) / (1+2*x)
x = np.linspace(-3,3,100)
x1 = x[x<-0.5]
y1 = g(x1)
x2 = x[x>-0.5]
y2 = g(x2)
plt.plot(y1, x1, y2, x2)
plt.show()
13.
x=9/2
14.
(-1, -1)
프로그래밍 실습
import numpy as np
import matplotlib.pyplot as plt
def f(x):
if x>=-1 and x<1:
return -x**2+2
if x>=1 and x<=3:
return x**2-4*x+3
if x>3 and x<=5:
return -x**2+8*x-14
x=np.linspace(-1,5,601)
fx=np.zeros_like(x) # x와 동일한 길이의 fx라는 배열 만들기.
#fx도 601개의 배열이 만들어지며, 0,0으로 초기화.
for i in range(0,601):
fx[i]=f(x[i])
#조건문으로 정의한 함수fx는 스칼라 입력만 처리할 수 있다.
#따라서 for문을 통해 하나씩 함수에 넣고 계산해야함.
plt.plot(x,fx,'.')
plt.show()