for i=1:10 x=linspace(0,10,101); plot(x,sin(x+i)); print(gcf,'-deps',strcat('plot',num2str(i),',ps')); end
时间: 2024-01-14 20:04:00 浏览: 29
你的代码是一个循环,每次迭代都会生成一个新的图形并保存为.eps文件。在这个循环中,你使用linspace函数创建了一个从0到10的等差数列x,然后使用sin函数绘制了图形sin(x+i),其中i是循环变量。最后,你使用print函数将当前图形保存为.eps文件。
需要注意的是,你在保存文件时使用了strcat函数来构建文件名,其中num2str(i)将循环变量i转换为字符串。这样每次迭代都会生成一个不同的文件名,例如'plot1.ps'、'plot2.ps'等。
如果你有关于这段代码的更多问题,请继续提问。
相关问题
import numpy as np import math from scipy import integrate def f(x): return math.sin(x)*math.sin(x) #复化梯形法 def func(a,b,n,f): x = np.linspace(a,b,n+1) sum1 = 0 h =(b-a)/n for i in range(补充代码1): 补充代码2 return sum1 #复化辛普森法 def func1(a,b,n,f): x = np.linspace(a,b,n+1) sum1 = 0 h =(b-a)/n for i in range(补充代码3): 补充代码4 return sum1 #复化科特斯法 def func2(a,b,n,f): x = np.linspace(a,b,n+1) sum1 = 0 h =(b-a)/n for i in range( 补充代码5 ): 补充代码6 return sum1 answer = func(0,1,100,f) answer1 = func1(0,1,100,f) answer2 = func2(0,1,100,f) print(answer,answer1,answer2) #integrate积分作为精确值 value, error = integrate.quad(f,0, 1) print(value,error) print("error: ", abs(answer-value), abs(answer1-value), abs(answer2-value))import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文 标签 plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 f=lambda x:np.sin(x)*np.sin(x) #向前差商 def fo_df(x,h): plt.plot([x-h,x],[f(x-h),f(x)],'r--',label='前') result= 补充代码7 return result #向后差商 def back_df(x,h): plt.plot([x,x+h],[f(x),f(x+h)],'k--',label='后') result= 补充代码8 return result#中心差商 def cen_df(x,h): a=(f(x-h)+f(x+h))/2 plt.plot([x-h,x+h],[f(x-h)+f(x)-a,f(x+h)+f(x)-a],'g--',label='中') result= 补充代码9 return result xx=np.linspace(-0.5,1.5,20) yy=f(xx) plt.plot(xx,yy) print('前',fo_df(0.5,0.5)) print('后',back_df(0.5,0.5)) print('中',cen_df(0.5,0.5)) plt.legend(loc='best') plt.show() xx=np.linspace(-1,1,20) yy=f(xx) plt.plot(xx,yy) print('前',fo_df(0.5,0.25)) print('后',back_df(0.5,0.25)) print('中',cen_df(0.5,0.25)) plt.legend(loc='best') plt.show()
代码中有一些没有补充的部分,我来帮你补充一下。
复化梯形法:
```python
def func(a,b,n,f):
x = np.linspace(a,b,n+1)
sum1 = 0
h =(b-a)/n
for i in range(n):
sum1 += (f(x[i])+f(x[i+1]))*h/2
return sum1
```
复化辛普森法:
```python
def func1(a,b,n,f):
x = np.linspace(a,b,n+1)
sum1 = 0
h =(b-a)/n
for i in range(n):
sum1 += h/6*(f(x[i])+4*f((x[i]+x[i+1])/2)+f(x[i+1]))
return sum1
```
复化科特斯法:
```python
def func2(a,b,n,f):
x = np.linspace(a,b,n+1)
sum1 = 0
h =(b-a)/n
for i in range(n//2):
sum1 += h/3*(f(x[2*i])+4*f((x[2*i]+x[2*i+1])/2)+f(x[2*i+1]))
return sum1
```
向前差商:
```python
def fo_df(x,h):
plt.plot([x-h,x],[f(x-h),f(x)],'r--',label='前')
result= (f(x)-f(x-h))/h
return result
```
向后差商:
```python
def back_df(x,h):
plt.plot([x,x+h],[f(x),f(x+h)],'k--',label='后')
result= (f(x+h)-f(x))/h
return result
```
中心差商:
```python
def cen_df(x,h):
a=(f(x-h)+f(x+h))/2
plt.plot([x-h,x+h],[f(x-h)+f(x)-a,f(x+h)+f(x)-a],'g--',label='中')
result= (f(x+h)-f(x-h))/(2*h)
return result
```
###function approximation f(x)=sin(x) ###2018.08.14 ###激活函数用的是sigmoid import numpy as np import math import matplotlib.pyplot as plt x = np.linspace(-3, 3, 600) # print(x) # print(x[1]) x_size = x.size y = np.zeros((x_size, 1)) # print(y.size) for i in range(x_size): y[i] = math.sin(2*math.pi*0.4*x[i])+ math.sin(2*math.pi*0.1*x[i]) + math.sin(2*math.pi*0.9*x[i]) # print(y) hidesize = 10 W1 = np.random.random((hidesize, 1)) # 输入层与隐层之间的权重 B1 = np.random.random((hidesize, 1)) # 隐含层神经元的阈值 W2 = np.random.random((1, hidesize)) # 隐含层与输出层之间的权重 B2 = np.random.random((1, 1)) # 输出层神经元的阈值 threshold = 0.005 max_steps = 1001 def sigmoid(x_): y_ = 1 / (1 + math.exp(-x_)) return y_ E = np.zeros((max_steps, 1)) # 误差随迭代次数的变化 Y = np.zeros((x_size, 1)) # 模型的输出结果 for k in range(max_steps): temp = 0 for i in range(x_size): hide_in = np.dot(x[i], W1) - B1 # 隐含层输入数据 # print(x[i]) hide_out = np.zeros((hidesize, 1)) # 隐含层的输出数据 for j in range(hidesize): # print("第{}个的值是{}".format(j,hide_in[j])) # print(j,sigmoid(j)) hide_out[j] = sigmoid(hide_in[j]) # print("第{}个的值是{}".format(j, hide_out[j])) # print(hide_out[3]) y_out = np.dot(W2, hide_out) - B2 # 模型输出 # print(y_out) Y[i] = y_out # print(i,Y[i]) e = y_out - y[i] # 模型输出减去实际结果。得出误差 ##反馈,修改参数 dB2 = -1 * threshold * e dW2 = e * threshold * np.transpose(hide_out) dB1 = np.zeros((hidesize, 1)) for j in range(hidesize): dB1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * (-1) * e * threshold) dW1 = np.zeros((hidesize, 1)) for j in range(hidesize): dW1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * x[i] * e * threshold) W1 = W1 - dW1 B1 = B1 - dB1 W2 = W2 - dW2 B2 = B2 - dB2 temp = temp + abs(e) E[k] = temp if k % 100 == 0: print(k) plt.figure() plt.plot(x, Y) plt.plot(x, Y, color='red', linestyle='--') plt.show()这个程序如何每迭代100次就输出一次图片
要每迭代100次输出一次图片,你需要在代码中添加相应的逻辑。以下是修改后的代码示例:
```python
import numpy as np
import math
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 600)
x_size = x.size
y = np.zeros((x_size, 1))
for i in range(x_size):
y[i] = math.sin(2*math.pi*0.4*x[i]) + math.sin(2*math.pi*0.1*x[i]) + math.sin(2*math.pi*0.9*x[i])
hidesize = 10
W1 = np.random.random((hidesize, 1))
B1 = np.random.random((hidesize, 1))
W2 = np.random.random((1, hidesize))
B2 = np.random.random((1, 1))
threshold = 0.005
max_steps = 1001
def sigmoid(x_):
y_ = 1 / (1 + math.exp(-x_))
return y_
E = np.zeros((max_steps, 1))
Y = np.zeros((x_size, 1))
for k in range(max_steps):
temp = 0
for i in range(x_size):
hide_in = np.dot(x[i], W1) - B1
hide_out = np.zeros((hidesize, 1))
for j in range(hidesize):
hide_out[j] = sigmoid(hide_in[j])
y_out = np.dot(W2, hide_out) - B2
Y[i] = y_out
e = y_out - y[i]
dB2 = -1 * threshold * e
dW2 = e * threshold * np.transpose(hide_out)
dB1 = np.zeros((hidesize, 1))
for j in range(hidesize):
dB1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * (-1) * e * threshold)
dW1 = np.zeros((hidesize, 1))
for j in range(hidesize):
dW1[j] = np.dot(np.dot(W2[0][j], sigmoid(hide_in[j])), (1 - sigmoid(hide_in[j])) * x[i] * e * threshold)
W1 = W1 - dW1
B1 = B1 - dB1
W2 = W2 - dW2
B2 = B2 - dB2
temp = temp + abs(e)
E[k] = temp
if k % 100 == 0:
plt.figure()
plt.plot(x, Y)
plt.plot(x, Y, color='red', linestyle='--')
plt.savefig(f'iteration_{k}.png')
plt.close()
plt.figure()
plt.plot(x, Y)
plt.plot(x, Y, color='red', linestyle='--')
plt.show()
```
在上述示例中,我添加了一个条件判断语句`if k % 100 == 0`来判断是否达到每100次迭代的条件。如果满足条件,则在该迭代结束后绘制并保存图片,文件名为`iteration_k.png`,其中`k`表示当前迭代次数。
请根据你的具体需求和编程环境进行相应的调整。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)