22 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alfred vor etwa 24 Stunden
Bearbeitet: Torsten vor etwa eine Stunde
Akzeptierte Antwort: Hassaan
In MATLAB Online öffnen
I am a beginner to MATLAB.
I tried but I'm getting errors.
Anybody, kindly help. Thank you.
Equations dx/dt=-2x
dy/dt=2x
Initial condition: x(t=0)=100
y(t=0)=0
tspan=[0 3];
x0=100;
y0=0;
dzdt = (dxdt dydt);
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
[tSol, zSol]=ode45(@odefun,tspan,x0);
plot(tSol, zSol);
function dzdt=odefun(t,z)
x = z(1);
y = z(2);
dxdt = -2*x;
dydt = 2*x;
dzdt = [dxdt;dydt];
end
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Akzeptierte Antwort
Hassaan vor etwa 24 Stunden
In MATLAB Online öffnen
% Define the time span and initial conditions
tspan = [0 3];
z0 = [100; 0]; % Initial conditions for x and y, combined into a single column vector
% Call the ODE solver
[tSol, zSol] = ode45(@odefun, tspan, z0);
% Plot the solutions for x and y over time
figure; % Open a new figure window
hold on; % Keep the plot for adding both x and y
plot(tSol, zSol(:,1), 'b-', 'LineWidth', 2); % Plot x with a thicker blue line
plot(tSol, zSol(:,2), 'r--', 'LineWidth', 2); % Plot y with a thicker red dashed line
xlabel('Time t', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('Solutions x and y', 'FontSize', 12, 'FontWeight', 'bold');
title('Solutions to the differential equations dx/dt = -2x and dy/dt = 2x', 'FontSize', 14, 'FontWeight', 'bold');
legend('x(t)', 'y(t)');
grid on; % Turn on grid
set(gca, 'FontSize', 10); % Set the font size for axes
hold off; % Release the plot hold
% Define the system of differential equations
function dzdt = odefun(t, z)
x = z(1);
y = z(2);
dxdt = -2 * x;
dydt = 2 * x;
dzdt = [dxdt; dydt];
end
4 Kommentare 2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Alfred vor etwa 21 Stunden
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2139591-how-do-i-construct-matrices-of-multiple-variables-and-plot-a-graph#comment_3218731
Thank you very much @Hassaan. Your answer is helpful.
Hassaan vor etwa 20 Stunden
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2139591-how-do-i-construct-matrices-of-multiple-variables-and-plot-a-graph#comment_3218866
@Alfred You are welcome.
Alfred vor etwa 8 Stunden
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2139591-how-do-i-construct-matrices-of-multiple-variables-and-plot-a-graph#comment_3219266
In MATLAB Online öffnen
How do I find the maximum value of x?
How do I find the value of x at y=2?
@Hassaan@Torsten
I saw a code about interpolation, so I just edited it. However, I don't know if it is the right code for the question, and I don't even understand it. Kindly assist me. Thank you.
tspan=[0 3];
x0=100;
y0=0;
z0=[x0;y0];
[tSol, zSol]=ode45(@odefun,tspan,z0);
function dzdt=odefun(t,z)
x = z(1);
y = z(2);
dxdt = -2*x;
dydt = 2*x;
dzdt = [dxdt;dydt];
end
plot(tSol, zSol);
% Finding the time at which the maximum value occured
zmax=max(zSol);
tmaz = interp1(tSol,zSol,zmax);
% Find max value over all elements.
maxz = max(zSol)
maxz = 1x2
100.0000 99.7521
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Get first element that is the max.
indexOfFirstMax = find(zSol == maxz, 1, 'first')
indexOfFirstMax = 1
% Get the x and y values at that index.
maxt = tSol(indexOfFirstMax)
maxt = 0
maxX = zSol(indexOfFirstMax)
maxX = 100
Torsten vor etwa eine Stunde
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2139591-how-do-i-construct-matrices-of-multiple-variables-and-plot-a-graph#comment_3219571
Bearbeitet: Torsten vor etwa eine Stunde
In MATLAB Online öffnen
tspan=[0 3];
x0=100;
y0=0;
z0=[x0;y0];
[tSol, zSol]=ode45(@odefun,tspan,z0);
function dzdt=odefun(t,z)
x = z(1);
y = z(2);
dxdt = -2*x;
dydt = 2*x;
dzdt = [dxdt;dydt];
end
How do I find the maximum value of x?
xmax = max(zSol(:,1))
xmax = 100
How do I find the value of x at y=2?
t2 = interp1(zSol(:,2),tSol,2); % find time at which y = 2
x2 = interp1(tSol,zSol(:,1),t2) % find x at which time = time at which y = 2
x2 = 98
Melden Sie sich an, um zu kommentieren.
Weitere Antworten (1)
Torsten vor etwa 24 Stunden
In MATLAB Online öffnen
tspan=[0 3];
x0=100;
y0=0;
z0=[x0;y0];
[tSol, zSol]=ode45(@odefun,tspan,z0);
plot(tSol, zSol);
function dzdt=odefun(t,z)
x = z(1);
y = z(2);
dxdt = -2*x;
dydt = 2*x;
dzdt = [dxdt;dydt];
end
1 Kommentar -1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Alfred vor etwa 21 Stunden
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2139591-how-do-i-construct-matrices-of-multiple-variables-and-plot-a-graph#comment_3218736
Thank you very much @Torsten. Your answer is helpful
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung