Cosine expression in C [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How can I write 3 cosĀ² 2x with C?

Assuming x is in radians :
3*pow(cos(2*x),2)

Here is a function for your purpose:
#include <math.h>
double f(double x) {
double c = cos(2 * x);
return c * c * 3;
}
First compute cos 2x, then square the result and multiply by 3.

Related

compute distance for clustering algorithm in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
Which could be a good method to compute the distance between a cluster and a point to decide if the point can be assigned to the cluster?
I read about Mahalanobis distance that I have implemented in this way:
float mahalanobis(float *cluster, float *point, const float *variance, int d) {
float distance=0;
for (int i = 1; i < d; ++i) {
distance+=pow((point[i] - cluster[i])/variance[i-1], 2);
}
return sqrt(distance);
}
but it seems that the result isn't good. Maybe I'm wrong about the implementation

Can I make a float number in a C program always round up [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Can I make a float number in a C program always round up
You can use the ceil() function. For example:
#include <stdio.h>
#include <math.h>
int main () {
float val1 = 1.6;
printf ("Round up to %.1lf\n", ceil(val1));
return(0);
}

how to handle a mathematical function in C language like MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm looking for a way to create a "variable-function" in C language.
In MATLAB i'm able to create something like:
my_function = (#x) sin(x) + x^2 + x;
so that i'm able to evaluate it for any value of 'x' i like:
my_point = 3.09;
my_function(my_point);
is there anything like that for C language?
That's just a regular C function. The terminology would be: "A function with arguments"
double my_function(double x)
{
return sin(x) + x*x + x;
}
#include <stdio.h>
#include<math.h>
#define my_function(x) sin(x) + pow(x,2) + x
int main()
{
double my_point = 3.09;
printf("%lf",my_function(my_point));
return(0);
}

How to calculate Determinant Matrix 2x2 -C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So I need to calculate the determinant matrix on 2x2 matrix, simple one..
If you can, please write the code, because i don't understand like in the other thread, it's like i'm reading an English Literature + Code..
#include<stdio.h>
int main(){
int x[2][2]={{1,2},{3,4}};
int i,j,d;
for(i=0;i<=1;i++){
for(j=0;j<=1;j++){
printf(" %d ", x[i][j]);
}
printf(" \n");
}
return 0;
}
Thank You!
You don't need a loop to calculate the determinant of a 2x2 matrix, simply use:
int result = (x[0][0] * x[1][1]) - (x[0][1] * x[1][0]);

Write c program to find mean of two numbers without using division [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I just wonder how to find mean of two numbers without using division.
do not use these conditions :
int mean = (a + b) >> 1;
four fundamental arithmetic operations
I think this may be helpful -->
int a,b,i,j;
if (a>b)
{
int temp = a;
a = b;
b = temp;
}
for(i=a,j=b;i<j;i++,j--)
continue;
if(i==j)printf("%d\n", i);
else printf("%lf\n", (double)(i)-0.5);
Add them then multiply by 0.5 , no division involved.
If they're both integers, you can use a right shift:
int median = (a + b) >> 1;

Resources