user735796
user735796

Reputation:

Example equation translation in C?

Originally I asked: How to tranlate equations to programming instructions. Judging by the content found @ Wikipedia: Category: Equations That's apparently too vague for simple Q&A. So I've deleted it.

Since I learn best by example, I would like to see what this equation looks like as a function in C.

an image

(desired course is d, ground speed is Vg, heading is a, true airspeed is Va, wind direction is w, wind speed is Vw. d, a and w are angles. Vg, Va and Vw are consistent units of speed. \pi is 3.14159...)

The above equation is for calculating true ground speed Taken from the E6B flight computer

To be clear: I'm not asking for advice, opinions or rhetoric. I'm not asking you to do my homework. I'm asking for help to understand the process of translation from equation to functional implementation. Bear with me, I know there are quite a few aspects to be aware of in such an endeavour, I would simply like to explore the symmetry between these two symbol systems using my own knowledge, capabilities and understanding.

Due to the fact that I would like to explore the symmetry, I should ask that you keep program identifiers as closely related to their representation in the formula as possible. Key words, catch phrases and rule references pertinent to this example translation in context (code comments) are a plus.

If my question is flawed due to an OVERSIGHT, please express your opinion in the comments below.

Upvotes: 2

Views: 204

Answers (2)

jjrv
jjrv

Reputation: 4345

You enter formulas very much like you would in a graphing calculator. The part with the cos would go like this:

#include <math.h>

int main(void) {
    double d, w, da;
    double result;

    result = cos( ( M_PI * ( d - w + da ) ) / ( 180 * M_PI / 180 ) );
}

You can probably add the rest of the formula quite easily. Of course you have to initialize all your variables with something reasonable before computing the result, and finally do something with it. Don't put it all on one line, use temporary variables (so in the next step you substitute the previous result for the entire cos expression) and split the calculation to parts that you can meaningfully describe in domain-specific terms, if possible.

Now regarding what to do differently in programming compared to a math publication, often other programmers won't be experts in your domain. I don't know at all for example what that formula does just by looking at it. Therefore I'd appreciate renaming the variables and adding comments relevant to the practical use of the formula (your question didn't originally explain what the variable names mean so I came up with very plausible ones). Like this:

#include <math.h>

int main(void) {
    double density, angledelta;
    double weight; /* Weight of cake with plate included. */
    double deg2rad;
    double result;

    deg2rad = M_PI / 180;

    /* Calculate X coordinate of next cake slice intersection with cake perimeter
       using fair cake splitting with density heuristics. */

    result = cos( ( M_PI * ( density - weight + angledelta ) ) / ( 180 * deg2rad ) );
}

Upvotes: 3

mikek3332002
mikek3332002

Reputation: 3562

To naively convert an equation into c you break it into parts based off the order of operations. Then you can refine it as needed.

The way I would break it down:

The function would have the parametres: d, w, deltaA, Va, Vw

  1. Step1 = d - w + ΔA

  2. Step2 = Math.PI * Step1

  3. Step3 = Step2 / 180 ignore the units(deg)

  4. Step4 = cos(Step3)

  5. Step5 = Va * Va

  6. Step6 = Vw * Vw

  7. Step7 = 2 * Va * Vw

  8. Step8 = Step5 + Step6 - Step7

  9. Step9 = Step8 * Step4

  10. Step10 = Math.sqrt(Step9)

  11. Return Step10

Upvotes: 2

Related Questions