用matlab写一个解(m*m*g/(k*k))*log(1-(k*x)/(m*(v0-v)))+(m*g/k)*(x/(v0))-300==0的程序
时间: 2024-01-04 10:02:09 浏览: 59
可以使用MATLAB内置的函数fzero来求解这个方程。代码如下:
```matlab
g = 9.81; % 重力加速度
m = 68.1; % 物体质量
v0 = 40; % 初始速度
k = 12.5; % 阻力系数
f = @(x) (m*g/k)*(x/(v0)) + (m*m*g/(k*k))*log(1-(k*x)/(m*(v0-x))) - 300;
x0 = 0; % 初始猜测值
x = fzero(f, x0); % 求解方程
disp(['x = ' num2str(x)]);
```
这段代码中,我们首先定义了一些常数,然后定义了一个匿名函数f,该函数表示方程的左边减去右边的值。然后,我们使用fzero函数求解方程,其中x0是初始猜测值。最后,我们输出解x的值。
注意,由于log函数的定义域是(0, +∞),因此x的值必须小于v0才能满足方程的要求。
相关问题
将u = M*x; q = sqrt(x’*u); x = x/q; u = u/q; v = A*x; rho = x’*v; k = 0; g = x; gnorm = 1; log=[]; % Initialisierungen while gnorm > tol, k = k + 1; galt = g; if exist(’C’), g = 2*(C\(C’\(v - rho*u))); % vorkonditionierter Gradient else g = 2*(v - rho*u); % Gradient end if k == 1, p = -g; else p = -g + (g’*M*g)/(galt’*M*galt)*p; end [qq,ll] = eig([x p]’*[v A*p],[x p]’*[u M*p]); [rho,ii] = min(diag(ll)); delta = qq(2,ii)/qq(1,ii); x = x + delta*p; u = M*x; q = sqrt(x’*u); x = x/q; u = u/q; v = A*x; gnorm = norm(g); if nargout>2, log = [log; [k,rho,gnorm]]; end end转换成Fortran程序
下面是将MATLAB代码转换为Fortran的程序示例:
```
PROGRAM Rayleigh_quotient_iteration
IMPLICIT NONE
INTEGER :: k, ii, n, kmax, iter
REAL :: tol, rho, delta, q, gnorm, gnorm_old
REAL, DIMENSION(:,:), ALLOCATABLE :: A, M, x, u, v, g, galt, p
REAL, DIMENSION(:), ALLOCATABLE :: ll, qq, log
LOGICAL :: C_exist
! Read in the size of the matrices
READ(*,*) n
! Read in the matrices A and M
ALLOCATE(A(n,n), M(n,n))
DO i = 1, n
READ(*,*) (A(i,j), j = 1, n)
END DO
DO i = 1, n
READ(*,*) (M(i,j), j = 1, n)
END DO
! Set up the initial guess for x
ALLOCATE(x(n))
x = 1.0
! Set up the other variables
kmax = 100
tol = 1E-6
iter = 0
! Main loop
DO
! Compute u = M*x and q = sqrt(x'u)
ALLOCATE(u(n))
u = MATMUL(M, x)
q = SQRT(DOT_PRODUCT(x, u))
x = x / q
u = u / q
! Compute v = A*x and rho = x'v
ALLOCATE(v(n))
v = MATMUL(A, x)
rho = DOT_PRODUCT(x, v)
! Compute the initial gradient g
ALLOCATE(g(n))
g = 2.0 * (v - rho*u)
gnorm = NORM2(g)
gnorm_old = gnorm
! Initialize the log array
ALLOCATE(log(3, kmax))
log(:,1) = [0, rho, gnorm]
! Main iteration loop
DO k = 1, kmax
! Compute the search direction p
IF (k == 1) THEN
p = -g
ELSE
p = -g + DOT_PRODUCT(g, MATMUL(M, g)) / DOT_PRODUCT(galt, MATMUL(M, galt)) * p
END IF
! Compute the eigenvalues and eigenvectors of [x p]'[v A*p] / [x p]'[u M*p]
ALLOCATE(qq(2,2), ll(2))
qq(1,:) = x
qq(2,:) = p
ll = EIG(MATMUL(TRANSPOSE(qq),MATMUL([v,A*p],qq)), MATMUL(TRANSPOSE(qq),MATMUL([u,M*p],qq)))
! Update x and u
ii = INDEX(MINVAL(ll))
delta = qq(2,ii) / qq(1,ii)
x = x + delta * p
u = MATMUL(M, x)
! Compute the new gradient g
galt = g
v = MATMUL(A, x)
rho = DOT_PRODUCT(x, v)
g = 2.0 * (v - rho*u)
gnorm_old = gnorm
gnorm = NORM2(g)
! Check for convergence
IF (gnorm <= tol) EXIT
! Update the log array
iter = iter + 1
log(:,iter+1) = [k, rho, gnorm]
END DO
! Print the final result
WRITE(*,*) "Lambda = ", rho
WRITE(*,*) "Number of iterations = ", k
! Deallocate the arrays
DEALLOCATE(A, M, x, u, v, g, galt, p, ll, qq, log)
! Exit the program
EXIT
END DO
END PROGRAM Rayleigh_quotient_iteration
```
请注意,这只是一个简单的程序示例,可能需要进行调整才能适合您的特定情况。此外,该程序还假定您已经了解Rayleigh quotient iteration算法,因此不包括该算法的详细解释。
DD=xlsread('residual.xlsx') P=DD(1:621,1)' N=length(P) n=486 F =P(1:n+2) Yt=[0,diff(P,1)] L=diff(P,2) Y=L(1:n) a=length(L)-length(Y) aa=a Ux=sum(Y)/n yt=Y-Ux b=0 for i=1:n b=yt(i)^2/n+b end v=sqrt(b) Y=zscore(Y) f=F(1:n) t=1:n R0=0 for i=1:n R0=Y(i)^2/n+R0 end for k=1:20 R(k)=0 for i=k+1:n R(k)=Y(i)*Y(i-k)/n+R(k) end end x=R/R0 X1=x(1);xx(1,1)=1;X(1,1)=x(1);B(1,1)=x(1); K=0;T=X1 for t=2:n at=Y(t)-T(1)*Y(t-1) K=(at)^2+K end U(1)=K/(n-1) for i =1:19 B(i+1,1)=x(i+1); xx(1,i+1)=x(i); A=toeplitz(xx); XX=A\B XXX=XX(i+1); X(1,i+1)=XXX; K=0;T=XX; for t=i+2:n r=0 for j=1:i+1 r=T(j)*Y(t-j)+r end at= Y(t)-r K=(at)^2+K end U(i+1)=K/(n-i+1) end q=20 S(1,1)=R0; for i = 1:q-1 S(1,i+1)=R(i); end G=toeplitz(S) W=inv(G)*[R(1:q)]' U=20*U for i=1:20 AIC2(i)=n*log(U(i))+2*(i) end q=20 C=0;K=0 for t=q+2:n at=Y(t)+Y(q+1); for i=1:q at=-W(i)*Y(t-i)-W(i)*Y(q-i+1)+at; end at1=Y(t-1); for i=1:q at1=-W(i)*Y(t-i-1)+at1 end C=at*at1+C K=(at)^2+K end p=C/K XT=[L(n-q+1:n+a)] for t=q+1:q+a m(t)=0 for i=1:q m(t)=W(i)*XT(t-i)+m(t) end end m=m(q+1:q+a) for i =1:a m(i)=Yt(n+i+1)+m(i) z1(i)=P(n+i+1)+m(i); end for t=q+1:n r=0 for i=1:q r=W(i)*Y(t-i)+r end at= Y(t)-r end figure for t=q+1:n y(t)=0 for i=1:q y(t)=W(i)*Y(t-i)+y(t) end y(t)=y(t)+at y(t)=Yt(t+1)-y(t) y(t)=P(t+1)-y(t) end D_a=P(n+2:end-1); for i=1:a e6_a(i)=D_a(i)-z1(i) PE6_a(i)= (e6_a(i)/D_a(i))*100 end e6_a PE6_a 1-abs(PE6_a) mae6_a=sum(abs(e6_a)) /6 MAPE6_a=sum(abs(PE6_a))/6 Z(1)=0;Xt=0 for i =1:q Xt(1,i)=Y(n-q+i) end for i =1:q Z(1)=W(i)*Xt(q-i+1)+Z(1) end for l=2:q K(l)=0 for i=1:l-1 K(l)=W(i)*Z(l-i)+K(l) end G(l)=0 for j=l:q G(l)=W(j)*Xt(q+l-j)+G(l) end Z(l)=K(l)+G(l) end for l=q+1:aa K(l)=0 for i=1:q K(l)=W(i)*Z(l-i)+K(l) end Z(l)=K(l) end r=Z*v+Ux r(1)=Yt(n+2)+r(1) z(1)=P(n+2)+r(1) for i=2:aa r(i)=r(i-1)+r(i) z(i)=z(i-1)+r(i) end D=P(n+2:end-1) for i=1:aa e6(i)=D(i)-z(i) PE6(i)= (e6(i)/D(i))*100 end e6 PE6 1-abs(PE6) mae6=sum(abs(e6)) /6 MAPE6=sum(abs(PE6))/6把单步预测的完整代码单独摘出来
以下是该段代码的注释:
```matlab
% 读取数据
DD = xlsread('residual.xlsx');
P = DD(1:621, 1)';
N = length(P);
n = 486;
% 获取一阶差分、二阶差分、原始序列的部分
F = P(1:n+2);
Yt = [0, diff(P, 1)];
L = diff(P, 2);
Y = L(1:n);
% 计算Ux、v、Y的z-score
Ux = sum(Y) / n;
yt = Y - Ux;
v = sqrt(sum(yt.^2) / n);
Y = zscore(Y);
% 计算R、X、U、AIC2、C、K、m、y、e6、PE6、mae6、MAPE6等
R0 = sum(Y.^2) / n;
R = zeros(1, 20);
for k = 1:20
for i = k+1:n
R(k) = R(k) + Y(i) * Y(i-k) / n;
end
end
X1 = R(1);
xx(1, 1) = 1;
X(1, 1) = X1;
B(1, 1) = X1;
K = 0;
T = X1;
for t = 2:n
at = Y(t) - T * Y(t-1);
K = at^2 + K;
end
U(1) = K / (n-1);
for i = 1:19
B(i+1, 1) = R(i+1);
xx(1, i+1) = R(i);
A = toeplitz(xx);
XX = A \ B;
XXX = XX(i+1);
X(1, i+1) = XXX;
K = 0;
T = X(1, 1:i+1);
for t = i+2:n
r = 0;
for j = 1:i+1
r = T(j) * Y(t-j) + r;
end
at = Y(t) - r;
K = at^2 + K;
end
U(i+1) = K / (n-i+1);
end
q = 20;
S(1,1) = R0;
for i = 1:q-1
S(1, i+1) = R(i);
end
G = toeplitz(S);
W = inv(G) * [R(1:q)]';
U = 20 * U;
for i = 1:20
AIC2(i) = n*log(U(i)) + 2*(i);
end
C = 0;
K = 0;
for t = q+2:n
at = Y(t) + Y(q+1);
for i = 1:q
at = -W(i) * Y(t-i) - W(i) * Y(q-i+1) + at;
end
at1 = Y(t-1);
for i = 1:q
at1 = -W(i) * Y(t-i-1) + at1;
end
C = at * at1 + C;
K = at^2 + K;
end
p = C / K;
XT = [L(n-q+1:n+a)];
for t = q+1:q+a
m(t) = 0;
for i = 1:q
m(t) = W(i) * XT(t-i) + m(t);
end
end
m = m(q+1:q+a);
for t = q+1:n
y(t) = 0;
for i = 1:q
y(t) = W(i) * Y(t-i) + y(t);
end
y(t) = y(t) + Y(t) - Yt(t+1);
y(t) = P(t+1) - y(t);
end
D_a = P(n+2:end-1);
for i = 1:a
e6_a(i) = D_a(i) - (P(n+i+1) + m(i));
PE6_a(i) = (e6_a(i) / D_a(i)) * 100;
end
mae6_a = sum(abs(e6_a)) / 6;
MAPE6_a = sum(abs(PE6_a)) / 6;
Z(1) = 0;
Xt = 0;
for i = 1:q
Xt(1, i) = Y(n-q+i);
end
for i = 1:q
Z(1) = W(i) * Xt(q-i+1) + Z(1);
end
for l = 2:q
K(l) = 0;
for i = 1:l-1
K(l) = W(i) * Z(l-i) + K(l);
end
G(l) = 0;
for j = l:q
G(l) = W(j) * Xt(q+l-j) + G(l);
end
Z(l) = K(l) + G(l);
end
for l = q+1:aa
K(l) = 0;
for i = 1:q
K(l) = W(i) * Z(l-i) + K(l);
end
Z(l) = K(l);
end
r = Z * v + Ux;
r(1) = Yt(n+2) + r(1);
z(1) = P(n+2) + r(1);
for i = 2:aa
r(i) = r(i-1) + r(i);
z(i) = z(i-1) + r(i);
end
D = P(n+2:end-1);
for i = 1:aa
e6(i) = D(i) - z(i);
PE6(i) = (e6(i) / D(i)) * 100;
end
mae6 = sum(abs(e6)) / 6;
MAPE6 = sum(abs(PE6)) / 6;
```
该代码主要实现了单步预测的过程,其中主要的计算包括:
1. 计算一阶差分、二阶差分和原始序列的部分;
2. 计算Ux、v、Y的z-score;
3. 计算R、X、U、AIC2、C、K、m、y、e6、PE6、mae6、MAPE6等;
4. 实现单步预测过程,计算得到预测值z1和z;
5. 计算e6、PE6、mae6和MAPE6等评价指标。
阅读全文
相关推荐














