“For loop” to plot graphs of functions
7 views (last 30 days)
Show older comments
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 Comments
KALYAN ACHARJYA
on 10 Jun 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));
Answers (2)
Anurag Ojha
on 12 Jun 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!
0 Comments
Dineshkumar
on 19 Aug 2024
Modify the script so that the plotting code on lines 5–8 execute only if doPlot is 1.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!