Convert first order transfer function to c code [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a simple first order transfer such as "3/s+3" or "tf(3,[1 3])" function and I would like to implement in c code. I have a C function that is called with the delta time since the last iteration:
double output(double input, double t); //usually, t is around 0.01 second
How do implement the transfer function 3/s+3 in C?

It's not just a matter of implementing 3/(s+3) directly.
You need to discretize it to the z-domain using an appropriate technique (forward euler, backward euler, tustin, zero-order hold) then implement the discrete version of the filter.
The following would be a simple version for the Tustin transformation.
As written, the state needs to be initialized and stored somewhere externally to this function.
double firstOrderLag(double input, double coeff, double dT, double *state){
// Function to implement the discretization of a continuous time first
// order lag sys = coeff/(s+coeff) using the Tustin (Bilinear) transformation.
double num = (1/(1+2/coeff/dT)); // numerator
double den = (1-2/coeff/dT)*num; // denominator
double temp;
double output;
temp = input - den*(*state);
output = num*(temp + (*state));
*state = temp;
return output;
}

Related

a complicate example using loop invariant [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
can anyone supply a complicate example using loop invariant example such as sum(int n) is so trivial that it can not show the power of loop invariant. I want a example that is not that obivious, and we can use method like loop invariant to solve it.
The Wikipedia example is quite good:
for (int i = 0; i < n; i++) {
x = y + z;
a[i] = 6 * i + x * x;
}
Two invariant can be moved (y + z and x * x). The advantage of this example is that after LICM has been applied, you can apply other optimizations on the code to have something very easy.
There are plenty on papers/slides/courses about that, you sure can find a satisfying example.

Function to return a specific value in a given input range [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to Write a function in C language whose output with respect to input should like this:
The above table is just for an example. The input is not limited to 25, and also the number of inputs in a particular range is X instead of 5. I cannot figure out how to do this?
Right now I don't have enough time write a better question ;). Please edit it if you found any mistake.
int f(int x, int X){
return (x + (X-1))/X;
}
int func(int x)
{
if(x%5 == 0)
return x/5;
else
return x/5 + 1;
}
What about an array of structs along
struct range {
int lo, hi, result;
}
Ask the user for X, then allocate an array with X instances of this struct,
#include <stdlib.h>
struct range *array = malloc (X * sizeof *array);
Now loop over X table rows asking for the lo, hi and result. The rest is left as an exercise...

Forbids comparison between pointer and integer [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
struct student
{
char am[7];/* èá íáé*/
float final_grade;
float exam_grade;
float essay_grade;
};
struct student mathites[1];
do{
printf("Dwse A.M.:");
scanf("%s",&mathites[j].am);
} while((mathites[j].am<8120000) || (mathites[j].am>812015));
I saw post like this, but everyone asks about comparison with "==" so the answer is strcmp,
something similar for this case?
something similar for this case?
This one is a little different since you're trying to check if a string is within a range of a numeric value.
A few points first:
struct student mathites[1];
if you're going to declare an array of one you may as well not make it an array.
char am[7];/* èá íáé*/
If the largest value you want to compare against is 7 characters "8120000" then you want your array sized 8 (to include space for the null terminator)
scanf("%s",&mathites[j].am);}
You don't need to use the & operator when getting a string, and you should use a limiter so you don't overflow your string
So taken this into account I'd say the code should be more like:
struct student
{
char am[8];/* èá íáé*/
float final_grade;
float exam_grade;
float essay_grade;
};
struct student mathites;
do {
printf("Dwse A.M.:");
scanf("%7s",mathites.am);}
Now your actual question, you need to convert the string to an number first, then check against the value:
long str_value = strtol(mathites.am, NULL, 10);
}
while((str_value <8120000) || (str_value >812015));

Forward slicing tool for the C language [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am looking for a forward slicing tool for the C language. When I searched in Google, I didn't find any result.
I would have liked to access the Wisconsin Program-Slicing Tool Version 1.1, but this tool wasn't distributed. Could any other tool provide the functionality of forward slicing C programs?
The open-source static analysis platform Frama-C has a slicing plug-in with impact analysis functionality.
The OP suggests an example where the impact of the initialization sum = 0; is being computed. The example is like this:
void main() {
int i = 1; int sum = 0;
while (i<11) {
sum = add(sum, i);
i = add(i, 1);
}
printf("sum = %d\n", sum);
printf("i = %d\n", i);
}
static int add(int a, int b)
{
return(a+b);
}
The command-line to use is:
frama-c-gui -val t.c
The check-mark in the left-hand side column tells the user that there are selected statements in function add, too. In the bottom right corner, the analyzer points out a few minor issues with this example from an academic article.

How to find square root of a number using a recursive function? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Basically i want to create a recursive function to implement this program in C
#include <stdio.h>
main()
{
float guess=1,num,num1;
int i;
printf("enter any number:\n");
scanf("%f",&num);
num1=num;
for (i=1;num1>1;i++,num1/=10); //to calculate no of digits in input
i=i/2;
printf("i:%d\n",i); //to make a better guess
for (;i>0;i--,guess*=10);
printf("guess = %f\n",guess);
for (i=1;i<=10;i++) //evaluate square root improving accuracy with each loop
{
guess=(guess+num/guess)/2;
}
printf("sqrt: %f\n",guess);
}
Something like this:
#include <math.h>
#include <float.h>
float MySqrt(float num, float prev)
{
float next = (prev+num/prev)/2;
if (fabs(next-prev)<FLT_EPSILON*next)
return next;
return MySqrt(num, next);
}
To call it, pass 1.0 as your initial guess for the prev parameter.
You can easily make this fail with a stack overflow by passing in bad data, but you probably aren't going to be tested on that in this assignment.

Resources