필터 지우기
필터 지우기

How to solve a differential equation with non-constant coefficients

조회 수: 6 (최근 30일)
arbia haded
arbia haded 2017년 6월 8일
댓글: darova 2020년 5월 16일
Hi all
I have equation like this dy/dx = a(x)*y + b
where : a(x) is non constant (a=1/x) and b is a vector (10000 rows)
how can I solve this equation using matlab !!
Someone answer me plz

답변 (3개)

Torsten
Torsten 2017년 6월 8일
Take a look at the example
ODE with Time-Dependent Terms
under
https://de.mathworks.com/help/matlab/ref/ode45.html
Best wishes
Torsten.
  댓글 수: 4
arbia haded
arbia haded 2017년 6월 12일
hi Torsten b is not dependent on x here is my code :
a = @(x) 1/x;
xdomain = [1 100];
%b = rand(10000,1);% aléatoire
load ('Bx');
load ('By');
load ('Bz');
b= sqrt(Bx.^2+By.^2+Bz.^2);
y = ones(10000,1);
[x,y] = ode45(@(x,y,a,b) a(x)*y - b ,rdomain,y,[],a,b);
plot(x,y)
Torsten
Torsten 2017년 6월 13일
So you want to solve the ODE
y'=y/x-sqrt(Bx^2+By^2+Bz^2)
for all combinations (Bx,By,Bz) from your loaded arrays ?
Then use a loop over the length of Bx:
a = @(x) 1/x;
xdomain = linspace(1,100,10);
y0=1;
%b = rand(10000,1);% aléatoire
load ('Bx');
load ('By');
load ('Bz');
for i=1:numel(Bx)
f=@(x,y) a(x)*y-sqrt(Bx(i)^2+By(i)^2+Bz(i)^2);
[x,y]=ode45(f,xdomain,y0);
yvec(i,:)=y(:);
end
Best wishes
Torsten.

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


arbia haded
arbia haded 2017년 6월 13일
hi Torsten Thank u very much for your help :) Yesterday I tried to simplify the problem, so I started with a very simple sinusoidal signal of the following form: b = A sin (2 pi f t), I calculated the solution of this equation analytically , I found this expression : y(x,t) = -A x pi f cos (2 pi f t), It is clear that the solution has a sinusoidal shape. But when I switched to the solution with matlab I didn't get the right solution. here is my code :
if true
A =1.25; % amplitude de la sinusoide
f =50; % frequence de la sinusoide
Fe =5000; % fréquence d'échantillonnage
t=0:1/ Fe :0.3; % base temps discretisée
B=A*sin (2* pi*f*t); % sinusoide à temps discret
b=diff(B)./diff(t); % dérivé
figure (1)
subplot(2,2,1),plot (B,'r') % affichage de la courbe
title ('signal sinusoidal')
grid on
subplot(2,2,2),plot(b,'m')
title ('la dérivée')
grid on
a = @(x) 1/x;
xdomain = linspace(1,100,10);
y0=1;
for i=1:numel(b)
f=@(x,y) -a(x)*y-b(i);
[x,y]=ode45(f,xdomain,y0);
yvec(i,:)=y(:);
end
subplot(2,2,3), plot(x,y)
end
Here I took the derivative of b as a second member (In reality I need the signal derivative). As an attachment you will find the result of simulation.

Francisco Rosales
Francisco Rosales 2020년 5월 15일
Hi all
I have equation like this Az'(t) = Bz(t)+ b
how can I solve this equation using matlab !!
Someone answer me plz
Thaks
Grace

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by