필터 지우기
필터 지우기

how to implement without using "function-end" command?

조회 수: 2 (최근 30일)
Hoon
Hoon 2016년 3월 12일
댓글: Steven Lord 2016년 3월 13일
I'll start with an example. All the codes are from this website, http://12000.org/my_notes/matlab_ODE/
I'm trying to figure out how to use ode45
function first_oder_ode
t=0:0.001:5; % time scalex
initial_x=0;
[t,x]=ode45( @rhs, t, initial_x);
plot(t,x);
xlabel('t'); ylabel('x');
function dxdt=rhs(t,x)
dxdt = 3*exp(-t);
end
end
Now I want to avoid using
function
end
Because I wanna see what's inside the function in the work space.
then I can simply write like
f=@(t,x)3*exp(-t)+x; %%%define first order ode
t=0:0.001:5; %%%time scalex
x_initial=0; %%%x initial condition
[t,x]=ode45(f,t,x_initial); %%%solving ODE
plot(t,x);
This is easy one for getting numerical solution for 1st order ODE
The problem is, applying this into ODE system.
The Matlab code is (it's already in the website)
function second_oder_ode
% SOLVE d2x/dt2+5 dx/dt - 4 x = sin(10 t)
% initial conditions: x(0) = 0, x'(0)=0
t=0:0.001:3; % time scale
initial_x = 0;
initial_dxdt = 0;
[t,x]=ode45( @rhs, t, [initial_x initial_dxdt] );
plot(t,x(:,1));
xlabel('t'); ylabel('x');
function dxdt=rhs(t,x)
dxdt_1 = x(2);
dxdt_2 = -5*x(2) + 4*x(1) + sin(10*t);
dxdt=[dxdt_1; dxdt_2];
end
end
How do I write this in another way without using "function - end"? is it possible?
  댓글 수: 1
Steven Lord
Steven Lord 2016년 3월 13일
Rather than making the outer function into a script, I recommend giving it some output arguments. This way you don't clutter the caller's workspace with temporary variables that are only needed to help the code compute the variables in which you're interested.

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

채택된 답변

Walter Roberson
Walter Roberson 2016년 3월 12일
rhs = @(t, x) [x(2); -5*x(2) + 4*x(1) + sin(10*t)];
  댓글 수: 2
Hoon
Hoon 2016년 3월 12일
so, I wrote like this
t=0:0.001:3; % time scale
initial_x = 0;
initial_dxdt = 0;
rhs = @(t, x) [x(2); -5*x(2) + 4*x(1) + sin(10*t)];
[t,x]=ode45( @rhs, t, [initial_x initial_dxdt] );
plot(t,x(:,1));
xlabel('t'); ylabel('x');
It doesn't work
Hoon
Hoon 2016년 3월 12일
OH!!! it works!!! I have to loose @!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by