“For loop” to plot graphs of functions

7 次查看(过去 30 天)
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 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2018-6-10
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));

请先登录,再进行评论。

回答(2 个)

Anurag Ojha
Anurag Ojha 2024-6-12
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 2024-8-19
Modify the script so that the plotting code on lines 5–8 execute only if doPlot is 1.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by