linear ordinary differential equations in matlab

4 views (last 30 days)
TT
TT on 14 Jun 2015
Edited: Jan on 14 Jun 2015
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)

Answers (1)

Jan
Jan on 14 Jun 2015
Edited: Jan on 14 Jun 2015
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.

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!