Reputation: 39
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.
Upvotes: 1
Views: 197
Reputation: 2227
Well, what you want is to generate "a lot" of data, and then make some analysis. I would actually implement it in two steps:
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.
Upvotes: 2