11 views (last 30 days)
Show older comments
HUST Student on 10 Jun 2018
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/404925-using-for-loop-to-plot-graphs-of-functions
Answered: Anurag Ojha on 20 Jun 2024 at 9:22
Open in MATLAB Online
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:
clear
syms w e d n A Z F Q;
w=pi;
e=0.5;
d=10;
n=1;
eqn1 = Z+A.*w./(1-Q.*(1+e))*cos(F)==0;
eqn2 = cos(F+2.*Q.*n.*pi)+Z.*((e-Q.*(1+e))./A.*w)==0;
eqn3 = A-1/(2.*w.*abs(sin(Q.*n.*pi))).*(((1-e).*Z).^2+(d.*w+2.*(1+e).*(1-Q).*Q.*n.*pi.*Z).^2).^0.5==0;
equations = [eqn1 eqn2 eqn3];
vars = [A Z F Q];
range = [0 10;-40 40;0 6;NaN NaN];
for A=0:0.5:10;
for i=1:5
temp=vpasolve(equations, vars, range, 'random', true);
sol(i,1)=temp.Z;
sol(i,2)=temp.F;
sol(i,3)=temp.Q;
end
figure (1);
subplot(2,2,1)
plot(A,sol(i,1));
xlabel('A');
ylabel('Z');
subplot(2,2,2)
plot(A,sol(i,2));
xlabel('A');
ylabel('F');
subplot(2,2,3)
plot(A,sol(i,2));
xlabel('A');
ylabel('Q');
end
Error using getEqnsVars (line 50) Expecting two arguments: a vector of equations and a vector of variables.
Error in sym/vpasolve (line 95) [eqns,vars] = getEqnsVars(varargin{1:N});
Error in Sucesso (line 15) temp=vpasolve(equations, vars, range, 'random', true);
I do not know how to solve this error, because in the three equations eqn1 eqn2 eqn3 A is a variable that in the loop assumes the constant values in the interval (0, 10) so in each loop step a system of three equations with three variables (Z, Q, F) supposed to be solved.
If someone can help me I would thank you a lot.
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Stephen23 on 10 Jun 2018
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/404925-using-for-loop-to-plot-graphs-of-functions#comment_577022
See also:
https://www.mathworks.com/matlabcentral/answers/404871-for-loop-to-plot-graphs-of-functions
Sign in to comment.
Sign in to answer this question.
Answers (1)
Anurag Ojha on 20 Jun 2024 at 9:22
Open in MATLAB Online
Hey
The error you are encountering is due to the fact that the vpasolve function expects the equations and variables to be symbolic expressions, not symbolic variables. To fix this, you need to substitute the symbolic variables with their corresponding values in each iteration of the loop.
You can refer to following code, I have taken certain assumtions, kindly modify as per your use case:
syms w e d n A Z F Q;
w = pi;
e = 0.5;
d = 10;
n = 1;
A_values = 0:0.5:10;
num_A_values = length(A_values);
sol = nan(num_A_values, 3); % Initialize with NaN to handle cases with no solution
for i = 1:num_A_values
A = A_values(i);
eqn1 = Z + A * w / (1 - Q * (1 + e)) * cos(F) == 0;
eqn2 = cos(F + 2 * Q * n * pi) + Z * ((e - Q * (1 + e)) / (A * w)) == 0;
eqn3 = A - 1 / (2 * w * abs(sin(Q * n * pi))) * (((1 - e) * Z)^2 + (d * w + 2 * (1 + e) * (1 - Q) * Q * n * pi * Z)^2)^0.5 == 0;
equations = [eqn1, eqn2, eqn3];
vars = [Z, F, Q];
range = [-40 40; 0 6; -1 1]; % Q is typically between -1 and 1 based on its typical physical range
% Solve the equations for the current value of A
try
temp = vpasolve(equations, vars, range);
% Handle the case where vpasolve might return multiple solutions or empty
if ~isempty(temp)
if isstruct(temp)
if isfield(temp, 'Z') && isfield(temp, 'F') && isfield(temp, 'Q')
sol(i, 1) = double(temp.Z);
sol(i, 2) = double(temp.F);
sol(i, 3) = double(temp.Q);
end
elseif iscell(temp) && ~isempty(temp{1})
temp = temp{1}; % Extract the first solution
if isfield(temp, 'Z') && isfield(temp, 'F') && isfield(temp, 'Q')
sol(i, 1) = double(temp.Z);
sol(i, 2) = double(temp.F);
sol(i, 3) = double(temp.Q);
end
end
end
catch
% Handle any unexpected errors by skipping to the next iteration
continue;
end
end
figure;
subplot(2, 2, 1)
plot(A_values, sol(:, 1), 'o-');
xlabel('A');
ylabel('Z');
title('Z vs A');
subplot(2, 2, 2)
plot(A_values, sol(:, 2), 'o-');
xlabel('A');
ylabel('F');
title('F vs A');
subplot(2, 2, 3)
plot(A_values, sol(:, 3), 'o-');
xlabel('A');
ylabel('Q');
title('Q vs A');
sgtitle('Solutions of the System of Equations');
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Signal ProcessingDSP System ToolboxStatistics and Linear AlgebraLinear Algebra
Find more on Linear Algebra in Help Center and File Exchange
Tags
- for loop
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office