linear ordinary differential equations in matlab

3 次查看(过去 30 天)
Dear all, Can anybody debug my code? I want to find an answer for a differential equation. Let's see the first code. It works. However, for the second code, MATLAB gives an error "Not enough inputs to inline function.".
Function:
function [xvalues, yvalues] = eulerss(f,x0,xn,y0,n)
%EULER: MATLAB function M-file that solve the
%ODE y’=f, y(x0)=y0 on [x0,y0] using a partition
%with n equally spaced subintervals
dx = (xn-x0)/n;
x(1) = x0;
y(1) = y0;
for k=1:n
x(k+1)=x(k) + dx;
y(k+1)= y(k) + f(x(k),y(k))*dx;
end
xvalues = x';
yvalues = y';
Implementation:
f=inline('sin(x*y)')
[x,y]=eulerss(f,0,1,pi,10)
plot(x,y)
second code... Function:
function [tvalues, zvalues] = eulers(F,t0,tn,z0,n)
%EULER: MATLAB function M-file that solve the
%ODE z'=F, z(t0)=0 on [t0,z0] using a partition
%with n equally spaced subintervals
dt=(tn-t0)/n;
t(1)=t0;
z(1)=z0;
for k=1:n
t(k+1)=t(k)+dt;
z(k+1)=z(k)+F(t(k),z(k))*dt;
end
tvalues=t
zvalues=z
implementation:
clc
clear all
a=1;
b=1;
c=0.1;
x=0.001;
F=inline('(b/c)*(x-z)')
[t,z]=eulers(F,0,1,0,10)
plot(t,z)

回答(1 个)

Jan
Jan 2015-6-14
编辑:Jan 2015-6-14
inline functions are an outdated method to define functions. Use the modern method of an anonymous function or write an own function for the formula.

类别

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