DE (f) has wrong solution

1 view (last 30 days)
Napsugár
Napsugár on 31 May 2023
Answered: Steven Lord on 31 May 2023
[~] = f_2_3a()
function [t, x, A, C, dA, dC, f, g]=f_2_3a()
f = @(t, d) [(2 - d(2) / 5.2) * d(1); -(10 - d(1) / 1.25) * d(2); (0.7 - (d(1) + d(2)) / 8) * sin(d(3))];
d0 = [42.8; 19; 7];
t=[0 20] ; %a diffegyenlet megoldásának időlépései
x= ode45(f, t, d0); %a diffegyenlet változóinak értékei az adott időpillanatokban
b=15;
A= linspace(2, 300, 20); %a A értékek mátrixa, ahol kiértékeljük az iránymezőt
C= linspace(5, 200, 10); %a C értékek mátrixa, ahol kiértékeljük az iránymezőt
[A, C] = meshgrid(A, C);
dA= (2 - b/5.2)*A; %a vektormező pontjainak A irányú megváltozásai
dC= (0.7 - (A + b)/8).* sin(C); %a vektormező pontjainak C irányú megváltozásai
f=figure; %az iránymezős ábra
g=quiver(A, C, dA, dC, 'Color','r','LineWidth',1.5);
xlabel('a','FontWeight','bold','Color','g');
ylabel('c','FontWeight','bold','Color','b');
end
  1 Comment
Dyuman Joshi
Dyuman Joshi on 31 May 2023
What is the correct solution then?
Are all the values used correct? Is the differential equation system you coded correct?

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 31 May 2023
Your code calls ode45 with an output argument but then doesn't use that output argument later in the code. In addition you're calling your main function with few enough output arguments that you could simply omit the ode45 call altogether and the only change it would make would be to speed up execution of your code.
In fact none of the lines of code inside your function before the ode45 call do anything useful in this context, as you can see from the fact that I removed them and the code still ran.
b=15;
A= linspace(2, 300, 20); %a A értékek mátrixa, ahol kiértékeljük az iránymezőt
C= linspace(5, 200, 10); %a C értékek mátrixa, ahol kiértékeljük az iránymezőt
[A, C] = meshgrid(A, C);
dA= (2 - b/5.2)*A; %a vektormező pontjainak A irányú megváltozásai
dC= (0.7 - (A + b)/8).* sin(C); %a vektormező pontjainak C irányú megváltozásai
f=figure; %az iránymezős ábra
g=quiver(A, C, dA, dC, 'Color','r','LineWidth',1.5);
xlabel('a','FontWeight','bold','Color','g');
ylabel('c','FontWeight','bold','Color','b');
If you want the quiver plot to show the solution to the system of ODEs, use it later in the code. MATLAB doesn't somehow automatically know you want to plot the solutions to the first and third ODEs that you solved with ode45. But if that's what you want (comparing the expressions in f and the expressions for dA and dC leads me to that conclusion) you may want to use deval to evaluate the solution at your selected points and use that in your visualization.

Tags

Community Treasure Hunt

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

Start Hunting!