“For loop” to plot graphs of functions

5 visualizzazioni (ultimi 30 giorni)
HUST Student
HUST Student il 9 Giu 2018
Risposto: Dineshkumar il 19 Ago 2024
I'm trying to write a matlab (for loop) code to produce the graphs: Q as a function of A, F as a function of A, Z as a function of A from the known functions f1, f2 and f3 Z = f1 (A, F, Q) F = f2 (A, Q, Z) A = f3 (Z, Q). I made several attempts that were unsuccessful, if anyone can help me I would thank you a lot.
  6 Commenti
KALYAN ACHARJYA
KALYAN ACHARJYA il 10 Giu 2018
As per of your code the plot having 1 x-axis (A) and 3 y-axes (Z, F, Q), clarify?
Also following two subplots are same-
subplot(2,2,2)
plot(A,sol(i,2));
subplot(2,2,3)
plot(A,sol(i,2));

Accedi per commentare.

Risposte (2)

Anurag Ojha
Anurag Ojha il 12 Giu 2024
Hello
To produce the graphs Q as a function of A, F as a function of A, and Z as a function of A, you can use a for loop in MATLAB. I am adding my code below for your reference, I have taken a simple example to show how it could be done. You can modify it according to your use case.
% Define the known functions
f1 = @(A, F, Q) A + F + Q;
f2 = @(A, Q, Z) A - Q + Z;
f3 = @(Z, Q) Z + Q;
% Define the range of A values
A = 1:0.1:10;
% Initialize arrays to store the results
Q_values = zeros(size(A));
F_values = zeros(size(A));
Z_values = zeros(size(A));
% Compute the values for Q, F, and Z for each A value
for i = 1:length(A)
Q_values(i) = f1(A(i), F_values(i), Q_values(i));
F_values(i) = f2(A(i), Q_values(i), Z_values(i));
Z_values(i) = f3(Z_values(i), Q_values(i));
end
% Plot the graphs
figure;
subplot(3, 1, 1);
plot(A, Q_values);
xlabel('A');
ylabel('Q');
title('Q as a function of A');
subplot(3, 1, 2);
plot(A, F_values);
xlabel('A');
ylabel('F');
title('F as a function of A');
subplot(3, 1, 3);
plot(A, Z_values);
xlabel('A');
ylabel('Z');
title('Z as a function of A');
I hope this helps!

Dineshkumar
Dineshkumar il 19 Ago 2024
Modify the script so that the plotting code on lines 5–8 execute only if doPlot is 1.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by