ode45 with an input

I'd like to integrate a function with a variable that is taken from the user. For example, to integrate the function below:
function dydt = test(t,y)
dydt = -t*y/sqrt(2-y^2);
,I would call it with something like [t,y] = ode45('test',[0:0.1:5],1), but if I want to integrate dydt = -ty/sqrt(2-y^2)*b, where b is taken from the user, how would I do that?
Here's what I've tried:
function dydt = test(t,y,b)
dydt = -ty/sqrt(2-y^2)*b;
,and calling it with [t,y] = ode45('test',[0:0.1:5],1,3), where b = 3 in the call, but it's giving me an error. Is there a way to integrate a function with a variable that is taken from the user?

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 6일

0 개 추천

tspan=[0:0.1:5]
[T,Y] = ode45(@(t,y),test(t,y,b),tspan,y0)

댓글 수: 1

Jan
Jan 2013년 5월 6일
편집: Jan 2013년 5월 6일
Some tiny modifications:
tspan = 0:0.1:5; % This is a vector already
b = 23.4; % Define b as wanted
[T,Y] = ode45(@(t,y) test(t,y,b), tspan, y0); % One comma removed

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2013년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by