# Using ode45 to solve a Non linear ode with multiple variables?

34 views (last 30 days)
Nivedita Tanksali on 26 Mar 2021
Commented: Nivedita Tanksali on 27 Mar 2021
I have the following script file that uses ode45 to solve an equation
clear all;
clc;
clf;
tic;
tspan = 0:0.0033:100;
a=55*(pi/180);
b=0;
k0 = [a; b];
[t,k] = ode45(@pend_k,tspan,k0);
K1 = k(:,1);
K2 = k(:,2);
plot(t,K2)
and I have the function, pend_k, that i call:
function kdot = pend_k(t,k)
kdot = [ k(2); (A*k(2) - B*k(1)) ];
end
But I need to define A and B, which involve Several Variables:
A = 2*(3*p1*t.^2 + 2*p2*t + p3)/(p1*t.^3 + p2*t.^2 + p3*t + p4); % t = tspan
% where,
p1 = 0.000000001906*a^3 + (-0.0000007948)*a^2 + 0.00009188*a + (-0.003481)
p2 = 0.00000915*a^2 + (-0.0009381)*a + 0.05331
p3 = (-0.0001542)*a^2 + (-0.006078)*a + (-2.089)
p4 = (0.9388)*a + 4.546
% and
B = ( ((77.4474)/(1 + (1/16)*a^2 + (11/3072)*a^4 + (173/737280)*a^6)).^2 + ((A.^2)/2) - ((6*p1*t + 2*p2)/(p1*t.^3 + p2*t.^2 + p3*t + p4)) )
How can I include A and B into pend_k?
Do I have to write a bunch of other functions within pend_k?

Sulaymon Eshkabilov on 26 Mar 2021
Here is the corrected completed code:
tic;
tspan = 0:0.0033:100;
a=55*(pi/180);
b=0;
k0 = [a; b];
[t,k] = ode45(@pend_k,tspan,k0);
K1 = k(:,1);
K2 = k(:,2);
plot(t,K2)
function kdot = pend_k(t,k)
a=55*(pi/180);
b=0;
p1 = 0.000000001906*a^3 + (-0.0000007948)*a^2 + 0.00009188*a + (-0.003481);
p2 = 0.00000915*a^2 + (-0.0009381)*a + 0.05331;
p3 = (-0.0001542)*a^2 + (-0.006078)*a + (-2.089);
p4 = (0.9388)*a + 4.546;
A = 2*(3*p1*t.^2 + 2*p2*t + p3)/(p1*t.^3 + p2*t.^2 + p3*t + p4); % t = tspan
B = ( ((77.4474)/(1 + (1/16)*a^2 + (11/3072)*a^4 + (173/737280)*a^6)).^2 + ((A.^2)/2) - ((6*p1*t + 2*p2)/(p1*t.^3 + p2*t.^2 + p3*t + p4)) );
kdot = [ k(2); (A*k(2) - B*k(1)) ];
end
##### 2 CommentsShowHide 1 older comment
Nivedita Tanksali on 27 Mar 2021
Okay! Thanks!

R2020b

### Community Treasure Hunt

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

Start Hunting!