Matlab问题求解
functionvarargout=lgkt(varargin)clc,clearx0=0;xn=0.5;y0=0;h=0.05;[y,x,k]=lgkt4j(x0,xn...
function varargout=lgkt(varargin)
clc,clear
x0=0;xn=0.5;y0=0;h=0.05;
[y,x,k]=lgkt4j(x0,xn,y0,h);
n=length(x);
fprintf(' i x(i) y(i) k(i)\n');
for i=1:n
fprintf('%2d %4.4f %4.4f %4.4f\n',i,x(i),y(i),k(i));
end
function z1=f1(x)
z1=exp(-x/2)+x^2-1;
function z=f(x,y)
z=0.5*(-y+x^2+4*x-1);
function [y,x,k]=lgkt4j(x0,xn,y0,h)
x=x0:h:xn;
n=length(x);
y1=x;
y1(1)=y0;
k1=x;
k1(1)=0;
for i=1:n-1
t=i*h;
K1=f(x(i),y1(i));
K2=f(x(i)+h/2,y1(i)+h/2*K1);
K3=f(x(i)+h/2,y1(i)+h/2*K2);
K4=f(x(i)+h,y1(i)+h*K3);
y1(i+1)=y1(i)+h/6*(K1+2*K2+2*K3+K4);
k1(i+1)=f1(t);
end
y=y1;
k=k1;
请问一下我运行出来为什么k跟y是一样的值 展开
clc,clear
x0=0;xn=0.5;y0=0;h=0.05;
[y,x,k]=lgkt4j(x0,xn,y0,h);
n=length(x);
fprintf(' i x(i) y(i) k(i)\n');
for i=1:n
fprintf('%2d %4.4f %4.4f %4.4f\n',i,x(i),y(i),k(i));
end
function z1=f1(x)
z1=exp(-x/2)+x^2-1;
function z=f(x,y)
z=0.5*(-y+x^2+4*x-1);
function [y,x,k]=lgkt4j(x0,xn,y0,h)
x=x0:h:xn;
n=length(x);
y1=x;
y1(1)=y0;
k1=x;
k1(1)=0;
for i=1:n-1
t=i*h;
K1=f(x(i),y1(i));
K2=f(x(i)+h/2,y1(i)+h/2*K1);
K3=f(x(i)+h/2,y1(i)+h/2*K2);
K4=f(x(i)+h,y1(i)+h*K3);
y1(i+1)=y1(i)+h/6*(K1+2*K2+2*K3+K4);
k1(i+1)=f1(t);
end
y=y1;
k=k1;
请问一下我运行出来为什么k跟y是一样的值 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询