Borrar filtros
Borrar filtros

chapra problem 25.7 (solving ode with euler method)

1 visualización (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Oct. de 2018
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.)

Más respuestas (0)

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by