chapra problem 25.7 (solving ode with euler method)

3 次查看(过去 30 天)
I want to make a code to solve the problem in the image ,but when i run the program i get the following errors and i can not understand the mistake,if anyone can help me to understand my code problem .
My code
function [t,y,z] =p25_7(f1,f2,t1,t2,y0,z0,h)
t=t1:h:t2;
t =t';
n=length(t);
y=zeros(n,1);
z=zeros(n,1);
y(1)=y0;
z(1)=z0;
for i=1:n-1
y(i+1) = y(i) + f1(t(i) , y(i))*h ;
z(i+1) = z(i) + f2(y(i) , z(i))*h ;
end
command window

采纳的回答

Walter Roberson
Walter Roberson 2018-10-13
You create inline('z') which is a function with one input, designated z, that returns the input itself. You also create inline('t-y'), which is function with two inputs, designated t and y, that returns the the first input minus the second input.
You pass those two inline functions into p25_7, where they are known as f1 and f2.
Inside p25_7 you invoke
y(i+1) = y(i) + f1(t(i) , y(i))*h ;
which tries to invoke f1 with two inputs. But f1 is a function that only accepts one input, so you fail.
You also have
z(i+1) = z(i) + f2(y(i) , z(i))*h ;
f2 is a function that accepts two inputs, and you are passing in two inputs, so that would succeed.
However... the naming that you use for the inputs would suggest that f1 would be more natural taking inputs t and y, and that f2 would be more natural taking inputs t and z, but your f1 takes z and your f2 takes t and y. This suggests you might perhaps be using the wrong order of functions.
You can pass to inline() a list of variable names, such as
inline('z', 't', 'z')
inline('t-y', 't', 'y')
Note: inline() functions have been recommended against since MATLAB 5.1, more than a decade ago. inline() works by eval() the expression and so is not as efficient for repeated use as anonymous functions are (as those can get accelerated.)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by