完整代码。
实验内容
1. 分别用欧拉法和四阶龙格库塔法求解如下:
(1)R=4K,C=1F,L=1H,仿真时间:0.03s
(2)R=280,C=8F,L=2H,仿真时间:0.1s
两组参数下电路的单位阶跃响应。(初始条 件均为零)。
熟悉并理解掌握MATLAB模型转换以及参数提取指令(针对上述系统写出它们的运行结果)
G=tf (num, den)
G1=tf(G) G=zpk (z, p, k)
G1=zpk (G) G=ss (A, B, C, D)
G3=ss (G)
[num, den] = tfdata (G)
[Z, P,K] = zpkdata (G)
[A, B, C, D] = ssdata (G)
1.参数提取:
% -----参数提取---------------- clear; clc; L = 2; C = 0.000008; R = 280; num = [1]; den = [L*C, R*C, 1]; [z,p,k] = tf2zp(num,den) [A,B,C,D] = tf2ss(num,den)
2.欧拉法求解(1)R=4K,C=1F,L=1H,仿真时间:0.03s :
function [z]=fun(X) R =4000; C =0.000001; L = 1; A = [0, 1/C;-1/L,-R/L]; B = [0;1/L]; z = A*X+B; end
clear; clc; % ---------------欧拉法------------- h = 0.0001; y = [0;0]; for i = 0:1:300 t = i*h; x(i+1) = t; k1 = fun(y); k2 = fun(y+h*k1); y = y + (k1 +k2)*h/2; output1(i+1) = y(1,1); end plot(x,output1) %------------四阶-龙格-库塔----------- % h = 0.0001; % y = [0;0]; % for i = 0:1:1000 % t = i*h; % x(i+1) = t; % k1 = fun(y); % k2 = fun(y+h*k1/2); % k3 = fun(y+h*k2/2); % k4 = fun(y+h*k3); % y = y + (k1 + 2*k2 +2*k3 + k4)*h/6; % output1(i+1) = y(1,1); % end % plot(x,output1)
运行结果如下:
四阶龙格库塔(2)R=280,C=8F,L=2H,仿真时间:0.1s:
function [z]=fun(X) R =280; C =0.000008; L = 2; A = [0, 1/C;-1/L,-R/L]; B = [0;1/L]; z = A*X+B; end
clear; clc; % ---------------欧拉法------------- % h = 0.0001; % y = [0;0]; % for i = 0:1:300 % t = i*h; % x(i+1) = t; % k1 = fun(y); % k2 = fun(y+h*k1); % y = y + (k1 +k2)*h/2; % output1(i+1) = y(1,1); % end % plot(x,output1) %------------四阶-龙格-库塔----------- h = 0.0001; y = [0;0]; for i = 0:1:1000 t = i*h; x(i+1) = t; k1 = fun(y); k2 = fun(y+h*k1/2); k3 = fun(y+h*k2/2); k4 = fun(y+h*k3); y = y + (k1 + 2*k2 +2*k3 + k4)*h/6; output1(i+1) = y(1,1); end plot(x,output1)
运行结果如下:
我的微信公众号名称:深度学习与先进智能决策
微信公众号ID:MultiAgent1024
公众号介绍:主要研究强化学习、计算机视觉、深度学习、机器学习等相关内容,分享学习过程中的学习笔记和心得!期待您的关注,欢迎一起学习交流进步!