我如何输入函数并在句柄函数中使用它

时间:2018-12-30 21:32:45

标签: matlab ode runge-kutta

在此代码中,用户输入ODE的系数,并且当所有系数都是数字代码时工作正常,但是我想将f输入为sin(2 * t)之类的函数,我如何输入f以在z2perim中使用它功能?

我尝试将输入f作为句柄函数,并使用不能解决问题的符号

clc; clear; close all;
disp('A Differential Equation look like:');
disp('ay"+by''+cy=f(t)');
disp('if your variables are time variant, please enter after @(t) as function handle!')
disp('Enter your Coefficients');
a=input('a= ');
b=input('b= ');
c=input('c= ');
f=input('f= ');
disp('Enter your T limiation (enter in this way: [start,end])= ')
t_input=input('');
disp('Enter y(0) condition= ');
y0_input=input('');
disp('Enter y''(0) condition= ');
ydot0_input=input('');
disp('Enter your desirable error= ');
error=input('');

h=nthroot(error,4);
t=t_input(1):h:t_input(2);
n=length(t);

z1perim = @(z1,z2,t) z2;
z2perim = @(z1,z2,t) -((c/a)*z1)-((b/a)*z2)+((1/a)*f);



z1=zeros(n,1);
z2=zeros(n,1);
z1(1)=y0_input;z2(1)=ydot0_input;



for ii = 1:n-1

    k1=z1perim(z1(ii),z2(ii),t(ii));
    l1=z2perim(z1(ii),z2(ii),t(ii));
    k2=z1perim(z1(ii)+(h*k1/2),z2(ii)+(h*l1/2),t(ii)+h/2);
    l2=z2perim(z1(ii)+(h*k1/2),z2(ii)+(h*l1/2),t(ii)+h/2);
    k3=z1perim(z1(ii)+(h*k2/2),z2(ii)+(h*l2/2),t(ii)+h/2);
    l3=z2perim(z1(ii)+(h*k2/2),z2(ii)+(h*l2/2),t(ii)+h/2);
    k4=z1perim(z1(ii)+(h*k3),z2(ii)+(h*l3),t(ii)+h);
    l4=z2perim(z1(ii)+(h*k3),z2(ii)+(h*l3),t(ii)+h);

    z1(ii+1)=z1(ii)+(k1+2*k2+2*k3+k4)*h/6;
    z2(ii+1)=z2(ii)+(l1+2*l2+2*l3+l4)*h/6;

end

plot(t,z1);grid on;hold on
plot(t,z2);

2 个答案:

答案 0 :(得分:0)

您应该可以使用feval,查看其文档。

如果将zperim2的定义更改为:

z2perim = @(z1,z2,t) -((c/a)*z1)-((b/a)*z2)+((1/a)*feval(f, t));

然后,您可以使用任意函数作为f的输入,例如

@(t)sin(2*t)

那应该给你你想要的东西: plot output with sinusoidal input

完整代码如下:

clc; clear; close all;
disp('A Differential Equation look like:');
disp('ay"+by''+cy=f(t)');
disp('if your variables are time variant, please enter after @(t) as function handle!')
disp('Enter your Coefficients');
a=input('a= ');
b=input('b= ');
c=input('c= ');
disp('Example of valid f: "@(t)sin(2*t)"');
f=input('f=');
disp('Enter your T limiation (enter in this way: [start,end])= ')
t_input=input('');
disp('Enter y(0) condition= ');
y0_input=input('');
disp('Enter y''(0) condition= ');
ydot0_input=input('');
disp('Enter your desirable error= ');
error=input('');

h=nthroot(error,4);
t=t_input(1):h:t_input(2);
n=length(t);

z1perim = @(z1,z2,t) z2;
z2perim = @(z1,z2,t) -((c/a)*z1)-((b/a)*z2)+((1/a)*feval(f, t));

z1=zeros(n,1);
z2=zeros(n,1);
z1(1)=y0_input;z2(1)=ydot0_input;

for ii = 1:n-1

    k1=z1perim(z1(ii),z2(ii),t(ii));
    l1=z2perim(z1(ii),z2(ii),t(ii));
    k2=z1perim(z1(ii)+(h*k1/2),z2(ii)+(h*l1/2),t(ii)+h/2);
    l2=z2perim(z1(ii)+(h*k1/2),z2(ii)+(h*l1/2),t(ii)+h/2);
    k3=z1perim(z1(ii)+(h*k2/2),z2(ii)+(h*l2/2),t(ii)+h/2);
    l3=z2perim(z1(ii)+(h*k2/2),z2(ii)+(h*l2/2),t(ii)+h/2);
    k4=z1perim(z1(ii)+(h*k3),z2(ii)+(h*l3),t(ii)+h);
    l4=z2perim(z1(ii)+(h*k3),z2(ii)+(h*l3),t(ii)+h);

    z1(ii+1)=z1(ii)+(k1+2*k2+2*k3+k4)*h/6;
    z2(ii+1)=z2(ii)+(l1+2*l2+2*l3+l4)*h/6;

end

plot(t,z1);grid on;hold on
plot(t,z2);

答案 1 :(得分:0)

我使用了您的代码,但是没有用 我将您的方法更改为类似

ft=@(t) eval(f);
z1perim = @(z1,z2,t) z2;
z2perim = @(z1,z2,t) (-c/a)*z1+(-b/a)*z2+(1/a)*ft(t);

到目前为止,它运行良好