“For loop” to plot graphs of functions

조회 수: 5 (최근 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 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