linear ordinary differential equations in matlab

조회 수: 3 (최근 30일)
TT
TT 2015년 6월 14일
편집: Jan 2015년 6월 14일
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