1

Trying to calculate at which frequencies voltage hits max, i am able to print the most recent max but there may be lower values of frequency in which it is able to max voltage.

I am able to get the highest or lowest freq by switching the loop from + to - from or to 1000000 in increments of 10.

Tried nested if statement inside of VO > voMax

#include <stdio.h> #include <conio.h> #include <math.h> #define PI 3.14f #define Vi 5 #define L 4.3e-4 #define C 5.1e-6 int getFreq(); long getResist(); float getVO(float XL, float XC, int R); float getXC(int f); float getXL(int f); int main() { long resist, freq, fMax; float XL, XC, VO, voMax; voMax = 0; fMax = 0; resist = getResist(); for (freq = 1000000; freq >= 0; freq -= 10) { XL = getXL(freq); XC = getXC(freq); VO = getVO(XL, XC, resist); if (1000000 == freq) { fMax = freq; voMax = VO; } else if (VO > voMax) { fMax = freq; voMax = VO; } } printf("VO = %f Frequency = %d\n", voMax, fMax); getch(); return 0; } float getXL(long f) { float XL; XL = 2 * PI * f * C; return XL; } float getXC(long f) { float XC; XC = 1 / (2 * PI * f * C); return XC; } float getVO(float XL, float XC, long R) { float VO; VO = (Vi * R) / sqrt((XL - XC) * (XL - XC) + R * R); return VO; } int getFreq() { int freq; freq = 0; printf("please enter a frequency:"); scanf("%d", &freq); return freq; } long getResist() { int resist; resist = 0; printf("please enter a resistance:"); scanf("%d", &resist); return resist; } 

I want the voltage to print max at multiple freq.

5
  • Not sure whether I got this right. To print local maxima, you have to compare a value with its predecessor and successor. If it's greater than both, you found a local maximum (and could print it immediately i.e. inside the loop). Commented May 22, 2019 at 5:16
  • function getXL() uses C instead of L, I assume ;) Commented May 22, 2019 at 5:16
  • You want to print a list of all freq (not only the last one) at which the global (not local) maximum occurs? Can you somehow define a maximum number of occurences? Commented May 22, 2019 at 5:20
  • By the way, I assume that this might be a homework assignment to calculate resonance frequency. If that is the case then try using math instead of trying via simulaiton. I.e. do not loop over imaginable frequencies, use the given values to calculate. Feel free to comment on my speculations. Commented May 22, 2019 at 5:50
  • If there is a circuit with resistor, capacity and inductivity involved, consider showing some ascii-art of that circuit. Some people here might know about electrical enginerring and help you with matching the code to the circuit. Commented May 22, 2019 at 5:54

1 Answer 1

2

Well, what you want is to generate "a lot" of data, and then make some analysis. I would actually implement it in two steps:

  1. Generate the data (and save it in an array or in a file)
  2. Do any analysis you need on that data.

After you get the desired result with this clear approach, you can move to the next step and try to optimize the algorithm, according to any optimization rule you need.


I want the voltage to print max at multiple freq.

I think you need a small code update. You have the following sequence:

voMax = 0; fMax = 0; resist = getResist(); for (freq = 1000000; freq >= 0; freq -= 10) { 

you should probably have:

fMax = 0; resist = getResist(); for (freq = 1000000; freq >= 0; freq -= 10) { voMax = 0; 

(I moved "voMax = 0;" inside the "for").

In that way, you can calculate max voltages for all frequencies, without interference from the other frequencies.

Sign up to request clarification or add additional context in comments.

14 Comments

What is calculated for each frequency is VO, VOmax is the maximum of that over all frequencies. Resetting it each time is the part of your (otherwise good answer) which I don't get. It seems to defeat the whole purpose...
As I understand (the desired behavior is not excellently described) the OP wants more than one voMax. He can do it in 2 ways: save all voMax somewhere (array, file), or reset voMax for each iteration. In the end he will have to decide what to do with all the values.
i feel like an array would be more suitable but its for a school assignment so maybe im just trying to do too much, im just trying to explore avenues. and ya thank you! were right about the XL function, i changed mine!
If you find anything unclear about OP, then please use your commenting privilege to clarify. I think this answer is based on a risky assumption.
To avoid the risky assumptions, I provided alternatives. OP can choose the suitable one according to his very specific needs.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.