A problem with recalling a function

5 views (last 30 days)
So I have a code, that calculate the output voltage at the three Nodes of an RC Circuit based on the user's Input frequncy.. ( one of three, 1Hz, 8Hz, or 20Hz) .. And the user had only 3 choices, And i designed it using the following errorflag
errorflag = 0;
while (errorflag == 0)
disp(' ')
disp ('Which Frequency do you want to use?')
disp (' (1) 1Hz ')
disp (' (2) 8Hz')
disp (' (3) 20Hz')
freq_choice=input('Please enter choice (1, 2, 3) > ');
disp(' ')
% check for valid input
if( freq_choice == 1 || freq_choice == 2 || freq_choice == 3)
errorflag =1;
else
disp(' *** Invalid choice ')
disp(' *** Please respond with a 1, 2, or 3 ')
disp(' *** Press ENTER to continue ' )
pause
end % if
end % while
Based on the user input, let's assume he chose 1Hz, the output would be a subplot of the output voltage at Node1, Node2, and Node 3.. with 1Hz frequncy.
Now I want to improve the code a bit, expanding the choices to 6 as shown below
errorflag = 0;
while (errorflag == 0)
disp(' ')
disp ('Which analysis do you want to make?')
disp (' (1) Using 1Hz freq across all three Nodes (1&2&3) ')
disp (' (2) Using 8Hz freq across all three Nodes (1&2&3)')
disp (' (3) Using 20Hz freq across all three Nodes (1&2&3)')
disp (' (4) At Node1 ONLY across all three Freq (1Hz & 8Hz & 20Hz)')
disp (' (5) At Node2 ONLY across all three Freq (1Hz & 8Hz & 20Hz)')
disp (' (6) At Node3 ONLY across all three Freq (1Hz & 8Hz & 20Hz)')
freq_choice=input('Please enter choice (1, 2, 3,4,5,6) > ');
disp(' ')
% check for valid input
if( freq_choice == 1 || freq_choice == 2 || freq_choice == 3 || freq_choice == 4 || freq_choice == 5 || freq_choice == 6)
errorflag =1;
else
disp(' *** Invalid choice ')
disp(' *** Please respond with a 1, 2, 3, 4, 5 or ')
disp(' *** Press ENTER to continue ' )
pause
end % if
end % while
And I want to design a function, that takes two inputs from the user .. freq ( the input frequency) and nd (the node number, 1 ,2 or 3) .. (I was not sure about posting the whole code, so I added (......) replacing the circuit formulation part, and left the part that's connected with the two vraibles i wasnt ( freq , nd)
function dc=requiredgraph (freq, nd)
% Declare the number of the nodes and Matricies from the circuit nodal formulation (G ,C ,B)
......
% Our simulation conditions { End time (T) / Number of Points (N) }
......
% The Initial Conditions
......
% Time Vector, based on the Number of Points (N)
.......
%Definining the Input Voltage based on Freq
for i=1:N
Vin(i)=cos(2*pi*freq*t(i));
end
% Solve For V using the Time March Technique
V(:,1)=Vo;
for i=2:N
V(:,i)=((C+G*dt))\(C*V(:,i-1)+B*Vin(i)*dt);
%plotting the function
figure
plot(t,V(nd,:));
title('The Output voltage at Node2, at Frequency of 1Hz');
xlabel('Time');
ylabel('The Output Voltage');
grid on
end
end
Now, if i want to recall this function to plot the graph of output at Node 2 ( nd=2) and Freq of 8Hz ( freq=8) .. How can I do this? Sorry for the long question, and please let me know if there is any missing info.
Thanks in advance. And help is highly appreictated
  2 Comments
Abdelrhman Abdelfatah
Abdelrhman Abdelfatah on 27 Nov 2021
I think i would aslo need to re formulate the figure title to be depending on the user input Freq and the picked nd(Node number), is that even possible?
Dr. Kelsey Joy
Dr. Kelsey Joy on 27 Nov 2021
I recommend revising how you prevent your user from proceeding based on their input. Here is some sample code to help you out with your errorflag portion of code: https://www.mathworks.com/matlabcentral/fileexchange/102674-educational-validating-user-inputs-numbers-and-strings?s_tid=prof_contriblnk

Sign in to comment.

Accepted Answer

Abdelrhman Abdelfatah
Abdelrhman Abdelfatah on 30 Nov 2021
Okay I fixed it, by making the output figure, and recalled the function name in another code, and it worked perfectly
function dc=requiredgraph (freq, nd)
% Declare the number of the nodes and Matricies from the circuit nodal formulation (G ,C ,B)
......
% Our simulation conditions { End time (T) / Number of Points (N) }
......
% The Initial Conditions
......
% Time Vector, based on the Number of Points (N)
.......
%Definining the Input Voltage based on Freq
for i=1:N
Vin(i)=cos(2*pi*freq*t(i));
end
% Solve For V using the Time March Technique
V(:,1)=Vo;
for i=2:N
V(:,i)=((C+G*dt))\(C*V(:,i-1)+B*Vin(i)*dt);
%plotting the function
dc=figure
plot(t,V(nd,:));
title('The Output voltage at Node2, at Frequency of 1Hz');
xlabel('Time');
ylabel('The Output Voltage');
grid on
end
end

More Answers (0)

Categories

Find more on Functions 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!