solving nonlinear diffrential equation using ode45

I have never met this kind of non-linear equation, and this is my current code
-------------------------------------
function homework1 = main1(t,X)
global m c k1 k3
A = [ -c/m -k1/m; 1 0 ] ;
f = 0.*((t>=0)-(t>=10))+ (3*cos(t^2)).*((t>10)-(t>=30))+(-10).*((t>30)-(t>=50));
F=[f/m;0];
C=X*[0 0;1 0] ;
B=C.^3;
homework1= [X(2);A*X-k3/m*B+F] ;
-------
clear all
close all
global m c k1 k3
m = 1; c = 0.02 ; k1 = 1.25; k3=0.05;
tspan= 0:0.1: 50 ;
X0 = [ -3, 5 ] ;
[ t X ] = ode45(@main1, tspan, X0);
-------
what I am specially struggling with is the
  1. 'x^3' term, I can't express the appropriate term
  2. plotting (x,t) as the 't' goes on
  3. using ode45 is required
please help me...

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 19 日
編集済み: Ameer Hamza 2020 年 3 月 19 日

0 投票

First, no need to use global variables. Using them is not a recommended practice. Second, just write your ODE inside the main1.
Write this in a script file
tspan= 0:0.1:50 ;
X0 = [5, -3] ;
[t, X] = ode45(@main1, tspan, X0);
plot(t,X)
And save the following function in the file "main1.m"
function homework1 = main1(t,X)
m = 1;
c = 0.02;
k1 = 1.25;
k3=0.05;
if t < 10
F = 0;
elseif t < 30
F = 3*cos(t.^2);
else
F = -10;
end
homework1= [X(2); -c/m*X(2)-k1/m*X(1)-k3/m*X(1).^3+F/m] ;
end

7 件のコメント

alsgud qor
alsgud qor 2020 年 3 月 19 日
um,so U r saying that to write them entirely to...User function 'main1.m'?or code tab?
sorry I m not a native speaker so I might got misunderstood
Ameer Hamza
Ameer Hamza 2020 年 3 月 19 日
Both are correct. But for most common use, follow the method In my updated answer.
alsgud qor
alsgud qor 2020 年 3 月 19 日
Thank U so much! so this might be the result right? I add some more thing to be more visible.
Just one more question please...because of the COVID-19, I have difficulty on my study....
and the thing U solved also required main1.m and sub1.m ....what could be the sub1.m? I think there is no need to use more than one function...?
Ameer Hamza
Ameer Hamza 2020 年 3 月 19 日
Yes, the output looks correct.
No, sub1.m is not required. You just need one function.
alsgud qor
alsgud qor 2020 年 3 月 19 日
thank U.the thing I just uploaded,Is it have the same process that u just code?
Ameer Hamza
Ameer Hamza 2020 年 3 月 19 日
alsgud, for your second file, the differential equation will be different. You first have to find the differential equation and then apply the code.
alsgud qor
alsgud qor 2020 年 3 月 19 日
thank U.I'll try and ask if I face difficulty directly

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2020 年 3 月 19 日

コメント済み:

2020 年 3 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by