“For loop” to plot graphs of functions

7 ビュー (過去 30 日間)
HUST Student
HUST Student 2018 年 6 月 9 日
回答済み: Dineshkumar 2024 年 8 月 19 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by