changing the ode inside ode45

조회 수: 2 (최근 30일)
Haya M
Haya M 2020년 11월 16일
Hi everyone, I'm trying to solve an ode using ode45 which comes basically from matrix second order ode.
My idea is to solve first for dudx(2)=(sin(x)-z)*u(1) then solve for dudx(2)=(cos(x)-z)*u(1) as well as
adding the new solution to the first one to build a matrix of these two solutions.
I've tried this but because I have a function with a variable x so it doesnt work as a parameter
inside the function hundle.
Any idea would be appreciated and here is my code:
clear all
z=-1;
N=10;
xspan = [0 2*pi];
u01 = [1 0];
u02 = [0 1];
opts = odeset('RelTol',1e-9,'AbsTol',1e-9);
[u1] = ode45(@(x,u) ode(x,u,z),xspan,u01,opts);
[u2] = ode45(@(x,u) ode(x,u,z),xspan,u02,opts);
x1 = linspace(0,2*pi,N);
y1 = deval(u1,x1);
y2 = deval(u2,x1);
y1 = y1(:,end);
y2 = y2(:,end);
Phi = [y1 , y2];
function dudx = ode(x,u,z)
dudx = zeros(2,1);
dudx(1) = u(2);
dudx(2) = (sin(x)-z)*u(1);
end

답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by