Function handle for 2D differential equation system

3 ビュー (過去 30 日間)
Michael Perret
Michael Perret 2018 年 11 月 20 日
編集済み: Michael Perret 2018 年 11 月 20 日
Hello,
I've a code from my teacher that should works for 2D differential equation system but I don't find how to write the function handle. Here is the code that I modified at
%% Simulate Euler Method
and at the bottom of the file. Here is the code
clear all; close all; clc;
% See at the bottom of the file to define the equation
%% Definition of the problem
x0 = [0;0];
dt = 0.2;
% We iterate 10'000 times
ntimes = 10000;
% Compute the corresponding times
t = [0:dt:(ntimes-1)*dt]';
% Get the dimensionality of the system
ndim = length(x0);
% Initialize the output
x = zeros(ntimes, ndim);
x(1,:) = x0;
%% Simulate Euler method
for i=1:ntimes-1
% We iteratively compute the next position after dt
%x(i+1,:) = x(i,:) + dt*Fdot(x(i,:),t(i))
x(i+1,1) = x(i,1) + dt*Fdotx(x(i,1),x(i,2),t(i))
x(i+1,2) = x(i,2) + dt*Fdoty(x(i,1),x(i,2),t(i))
end
% Analytical solution (previous exercise)
%Fsol=x0.*exp(0.5)*exp(-0.5.*exp(-2*t));
figure
plot(t,x)
%hold on
%plot(t, Fsol, 'r')
%axis([0 10 0 2])
function f_x = Fdotx(x,y,t)
f_x = -3.*x+6.*y;
end
function f_y = Fdoty(x,y,t)
f_y = -2.*x-3.*y;
end
Is there something that I do completly wrong ?
The Fsol is for a previous exercice, so it is normal that's not the result.
Thanks in advance :)

回答 (0 件)

カテゴリ

Help Center および File ExchangeSpectral Measurements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by