CS计算机代考程序代写 matlab close all

close all
clear all

tmin=0.0;
tmax=8.0;

x0=[1 1];
Delta_t=0.2;

[t,x]=MyEulerLinearSysODEs([tmin tmax],x0,Delta_t);
[tmat,xmat]=ode23(@f,[0 8],[1 1]);

hold off
plot(t,x(:,2),’bo-‘,’linewidth’,2,’markersize’,10)
hold on
plot(tmat,xmat(:,2),’k-‘,’linewidth’,2)
xlabel(‘t’);
ylabel(‘x_1(t)’);
legend(‘Implicit Euler’,’MATLAB ode23()’)

function [t,x]=MyEulerLinearSysODEs(tspan,x0,Delta_t)

a=tspan(1);
b=tspan(2);

t=a:Delta_t:b;

x=zeros(length(t),numel(x0));

x(1,:)=x0; %setting initial conditions

K=[-6 -3;5 2];
temp=inv(eye(2)-K*Delta_t);

for n=1:length(t)-1
x(n+1,:)=(temp*x(n,:)’)’;
end
end

function dxdt=f(t,x)
dxdt=[-6*x(1)-3*x(2) ;5*x(1)+2*x(2)];
end