简体   繁体   中英

How can i input a function and use it in a handle function

in this code user input coefficients of ODE, and when all of Coefficients is number code work fine, but i want to input f as a function like sin(2*t), how can i input f to use it in z2perim function?

i try for input f as a handle function and use symbolic which it's not solve the problem

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);

You should be able to use feval , check its documentation.

If you change the definition of zperim2 to:

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

Then you can use an arbitrary function as input for f, eg,

@(t)sin(2*t)

That should get you what you want: 使用正弦输入绘制输出

The full code looks like this:

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);

i use your code but it's not working i change your method to something like this

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

and till now it's working perfect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM