pho=[300;862;74.2;1.18];%密度 c=[1377;2100;1726:1005];%比热容 lamda=[0.082;0.37;0.045;0.028];%热导率 a=lamda./pho./c;%热扩散率如何改成数组大小兼容
时间: 2023-07-28 16:07:55 浏览: 215
可以使用 `reshape` 函数将 `pho` 和 `c` 调整为和 `lamda` 相同的大小,然后再进行计算,代码如下:
```
pho = [300; 862; 74.2; 1.18];
c = [1377; 2100; 1726; 1005];
lamda = [0.082; 0.37; 0.045; 0.028];
pho_resized = reshape(pho, [], 1);
c_resized = reshape(c, [], 1);
a = lamda ./ pho_resized ./ c_resized;
```
在上面的代码中,`[]` 表示自动计算数组大小,以保证 `pho` 和 `c` 能够和 `lamda` 相匹配。
相关问题
a=lamda./(pho.*c)
这个表达式的含义是将 lambda 向量的每个元素除以 pho 向量和 c 向量对应位置的元素的乘积,结果是一个新的向量。
假设 lambda、pho 和 c 都是向量,那么在 Matlab 中可以这样写:
```
a = lambda ./ (pho .* c);
```
其中,./ 表示对每个元素进行除法运算。
function P137_72 clear all close all clc xk=0.5; alphamax=2; [alpha, newxk, fk, newfk] = wolfe(xk, dk) function [alpha, newxk, fk, newfk] = Wolfe(xk, dk) alpha = Wolfe(@phi,@gphi,a0,b0,t,pho) rho = 0.1; sigma = 0.8; alpha = 1; a1=0; a2=alphamax; dk=60; phi1=feval(phi, xk); gphi1=feval(phi, xk)'*dk; function f=phi(t) f=-2*t^3+21*t^2-60*t+50; % ÌݶÈ/µ¼Êý function gf=gphi(t) gf=-6*t^2+42*t-60;
以下是转换后的Python代码:
```python
import numpy as np
def phi(t):
return -2*t**3 + 21*t**2 - 60*t + 50
def gphi(t):
return -6*t**2 + 42*t - 60
def Wolfe(xk, dk):
alpha = 1
rho = 0.1
sigma = 0.8
alphamax = 2
a1 = 0
a2 = alphamax
phi1 = phi(xk)
gphi1 = gphi(xk)*dk
while True:
phialpha = phi(xk + alpha*dk)
if phialpha > phi1 + rho*alpha*gphi1 or (phialpha >= phi(a1 + alpha*(a2-a1)) and a2-a1 >= 10**(-10)):
alpha = zoom(a1, alpha, a2, xk, dk)
break
gphialpha = gphi(xk + alpha*dk)*dk
if abs(gphialpha) <= -sigma*gphi1:
break
if gphialpha >= 0:
alpha = zoom(alpha, alpha/2, alpha, xk, dk)
break
a1 = alpha
a2 = min(alphamax, alpha*2)
alpha = 2*alpha
newxk = xk + alpha*dk
fk = phi1
newfk = phi(newxk)
return alpha, newxk, fk, newfk
def zoom(a_lo, a_hi, xk, dk):
rho = 0.1
sigma = 0.8
phi1 = phi(xk)
gphi1 = gphi(xk)*dk
while True:
alpha = (a_lo + a_hi)/2
phialpha = phi(xk + alpha*dk)
if phialpha > phi1 + rho*alpha*gphi1 or phialpha >= phi(xk + a_lo*dk):
a_hi = alpha
else:
gphialpha = gphi(xk + alpha*dk)*dk
if abs(gphialpha) <= -sigma*gphi1:
break
if gphialpha*(a_hi - a_lo) >= 0:
a_hi = a_lo
a_lo = alpha
return alpha
xk = 0.5
alphamax = 2
alpha, newxk, fk, newfk = Wolfe(xk, dk)
```
阅读全文