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.
freq(not only the last one) at which the global (not local) maximum occurs? Can you somehow define a maximum number of occurences?