I have written following code in order to produce simple list of double pairs to import in plot program.
#include <stdio.h>
#include <math.h>
int main(void)
{
int i;
double x=2,y=3;
for(i = 0; i < 1000; i++){
x = y- x/fabs(x)*sqrt(fabs(x+0.7));
y = 0.3-x;
printf("%5.4f , %5.4f\n" ,x,y);
}
return 0;
}
I don't get what I expect from this functions. Instead of hopalong fractal I get linear progression graph. Is this only syntax error?
When you assign y, you use the new value of x, which has just been updated. The calculation requires the x value from the last step. Make a copy and use that:
int main(void)
{
double x = 2;
double y = 3;
int i;
for(i = 0; i < 1000; i++) {
double xx = x;
x = y - x/fabs(x)*sqrt(fabs(x + 0.7));
y = 0.3 - xx;
printf("%5.4f , %5.4f\n" ,x,y);
}
return 0;
}
Related
I'm trying to parallelize this code (changing only the main, not the heavy function) using mpi- with a dynamic task pool approach. i would like some help with the implementation. which parameters should i pass between master and slaves? how?
thank you
#include <stdio.h>
#include <math.h>
#define HEAVY 1000
#define SIZE 40
#define RADIUS 10
// This function performs heavy computations,
// its run time depends on x and y values
// DO NOT change the function
double heavy(int x, int y) {
int i, loop;
double sum = 0;
if (sqrt((x - 0.75*SIZE)*(x - 0.75*SIZE) + (y - 0.25*SIZE)*(y - 0.25*SIZE)) < RADIUS)
loop = 5*x*y;
else
loop = y + x;
for (i = 0; i < loop*HEAVY; i++)
sum += sin(exp(cos((double)i / HEAVY)));
return sum;
}
// Sequencial code to be parallelized
int main(int argc, char *argv[]) {
int x, y;
int size = SIZE;
double answer = 0;
for (x = 0; x < size; x++)
for (y = 0; y < size; y++)
answer += heavy(x, y);
printf("answer = %e\n", answer);
}
i tried to create a task pull in the size of 'size', but i didnt know what should be the values inside it and how to pass it right
Task: According to the Taylor Series of sin(x) calculate with using a double function named mysin pass it to a double variable. Take a x value from user and use the mysin function to calculate sin(x).
Problem is program gives me wrong value of sin(x). I have been trying to solve that issue about 4 hours but couldn't find it. Is it because of sin(x) function or have I missed something in my code?
My Code:
#include <stdio.h>
double mysin(double x)
{
double value = x;
double sum = x;
int neg_pos = 1;
int fac = 1;
int counter = 0;
while(1)
{
neg_pos *= -1;
fac += 2;
value = value/(fac*(fac-1));
value = value*x*x*neg_pos;
sum += value;
//printf("Hello");
counter++;
if (counter == 100) break;
}
return sum;
}
int main()
{
double number;
scanf("%lf",&number);
printf("%g",mysin(number));
//printf("%g",number);
}
The problem is that you're multiplying by neg_pos each step, which toggles between +1 and -1. That means the terms change sign only half the time, whereas they should change sign each time.
The fix is to just multiply by -1 each time rather than neg_pos.
Here's a working, slightly-simplified form of your program that calculates sin for a range of numbers from 0 to 3, showing the stdlib calculation to compare.
#include <math.h>
#include <stdio.h>
double mysin(double x) {
double value = x;
double sum = x;
int fac = 1;
for (int counter = 0; counter < 100; counter++) {
fac += 2;
value = -value*x*x/fac/(fac-1);
sum += value;
}
return sum;
}
int main() {
for (double x = 0.0; x < 3.0; x += 0.1) {
printf("%g: %g %g\n", x, mysin(x), sin(x));
}
}
You can also avoid the separate fac and counter variables, perhaps like this:
double mysin(double x) {
double term=x, sum=x;
for (int f = 0; f < 100; f++) {
term = -term*x*x/(2*f+2)/(2*f+3);
sum += term;
}
return sum;
}
From what I'm understanding you are not calculating the power correctly
Firtsly use this:
#include <math.h>
Then create a factorial function:
int factorial(int x){
int result = 1;
for(int i = 1; i < x; i++){
result += result * i;
}
return result;
}
And finally:
while(1)
{
neg_pos *= -1;
fac += 2;
power = pow(x,fac);
fac = factorial(fac);
sum += power/fac;
//printf("Hello");
counter++;
if (counter == 100) break;
}
I have been having some problems with the segment of code below.
#include "stm32f0xx_tim.h"
#include "stm32f0xx_adc.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_conf.h"
#include "adc.h"
void calcSensor(float voltage1, float voltage2, int X, int Y)
{
float Iload = 0;
float Vsensor = 0;
float Rsensor = 0;
float Vdrop = voltage1 - voltage2;
uint32_t resistance = 0;
Iload = Vdrop/Rload;
Vsensor = Vin - Iload*Rmux - Iload*Rdemux-Vdrop;
resistance = Vsensor/Iload;
Rsensor[1][5] = resistance;
Y++;
if (Y == 22)
{
Y = 0;
X++;
if (X == 44)
{
X = 0;
}
}
}
void initRArray(void)
{
int x;
int y;
for(x = 0; x < 44; x++)
{
for(y = 0; y < 22; y++)
{
Rsensor[x][y] = 0;
}
}
}
The error comes the line:
Rsensor[1][5] = resistance;
The error is the same as the title:
subscripted value is neither array nor pointer
I originally had X and Y for indicies but have switched to 0 and 5 thinking it may have been an issue. That did not fix it. Additionally, I have the intRarray function which sets all values to 0. This array compiles fine, and it is using the same array that is having issues.
Below is the declaration of the array in the header file.
unsigned long int Rsensor[44][22];
You have a local variable float Rsensor = 0; which shadows the global array. Rename one of the two.
You have the following declaration in the program
float Rsensor = 0;
This makes Rsensor a float variable, not an array.
I'm trying to print the Cantor Set to the console using 'x', but I'm stuck at the 2nd recursion which no matter what I do, just doesn't execute.
The Idea is to first initialize the matrix using clearP() so I don't have to worry about the whitespaces. After that I load the array with 'x' chars using the depth as a [y] value.
To remove the middle segment on each line I use secondLength and smallerLength. Now the reason to use 2 recursive calls is, that for example on depth 1 it removes the middle part once, on depth 2 twice, on depth 3 four times and so on. However I just can't get the 2nd recursion to execute, which is why my output looks like this.
Any advice where I'm making mistakes?
#include <stdio.h>
#include <math.h>
#define WIDTH 27
#define HEIGHT (int)(cbrt(WIDTH)+1)
void clearP(char p[WIDTH][HEIGHT]){
int x, y;
for(x = 0; x<WIDTH; x++){
for (y=0;y<HEIGHT;y++){
p[x][y] = ' ';
}
}
}
void printP(char p[WIDTH][HEIGHT]){
int x, y;
for(y = 0; y<HEIGHT; y++){
for (x=0;x<WIDTH;x++){
printf("%c",p[x][y]);
}
printf("\n");
}
}
void cantor(char p[WIDTH][HEIGHT],int start,int end, int depth){
int smallerLength = end / 3;
int secondStart = start + (smallerLength * 2);
for (int x = start; x<end ; x++){
p[x][depth] = 'x';
}
if (depth == HEIGHT){
return;
}
cantor(p, start, smallerLength, depth+1);
cantor(p, secondStart, smallerLength, depth+1);
}
int main(){
char canvas[WIDTH][HEIGHT];
clearP(canvas);
cantor(canvas, 0, WIDTH, 0);
printP(canvas);
}
I think you got your height and width mixed up in print.
try this
void printP(char p[WIDTH][HEIGHT]){
int x, y;
for(x = 0; x<HEIGHT; x++){
for (y=0;y<WIDTH;y++){
printf("%c",p[x][y]);
}
printf("\n");
}
}
A point in [0, 1] is in the Cantor set if it's ternary representation doesn't contain any 1's (that is, only 0's and 2's). This observation allows you to output the d-level representation by looking at the first d digits of the fractional part of i/n in base 3, without needing arrays.
#include <stdio.h>
void cantor(int n, int d) {
for (int i = 0; i < n; i++) {
int in = 1;
int x = i;
for (int j = 0; j < d; j++) {
in = in && !(3*x >= n && 3*x < 2*n);
x = (3*x)%n;
}
putchar(in ? 'x' : ' ');
}
putchar('\n');
}
int main(int argc, char *argv[]) {
for (int d = 0; d < 5; d++) {
cantor(81, d);
}
return 0;
}
I'm trying to implement the Mandelbrot set in C, but I'm having a weird problem. My code is as follows:
#include <stdio.h>
#include <math.h>
#include <complex.h>
int iterate_pt(complex c);
int main() {
FILE *fp;
fp = fopen("mand.ppm", "w+");
double crmin = -.75;
double crmax = -.74;
double cimin = -.138;
double cimax = -.75; //Changing this value to -.127 fixed my problem.
int ncols = 256;
int nrows = 256;
int mand[ncols][nrows];
int x, y, color;
double complex c;
double dx = (crmax-crmin)/ncols;
double dy = (cimax-cimin)/nrows;
for (x = 0; x < ncols; x++){
for (y = 0; y < nrows; y++){
double complex imaginary = 0+1.0i;
c = crmin+(x*dx) + (cimin+(y*dy)) * imaginary;
mand[x][y] = iterate_pt(c);
}
}
printf("Printing ppm header.");
fprintf(fp, "P3\n");
fprintf(fp, "%d %d\n255\n\n", ncols, nrows);
for (x = 0; x < ncols; x++) {
for (y = 0; y < nrows; y++){
color = mand[x][y];
fprintf(fp, "%d\n", color);
fprintf(fp, "%d\n", color);
fprintf(fp, "%d\n\n", color); //Extra new line added, telling the ppm to go to next pixel.
}
}
fclose(fp);
return 0;
}
int iterate_pt(double complex c){
double complex z = 0+0.0i;
int iterations = 0;
int k;
for (k = 1; k <= 255; k++) {
z = z*z + c;
if (sqrt( z*conj(z) ) > 50){
break;
}
else
++iterations;
}
return iterations;
}
However, the output of this program, which is stored as a ppm file looks like this:
Thanks for your help!
Try setting cimax to -0.127, I'm also working on this project and it seems to do the trick ;)
The code looks good.
But your starting rectangle doesn't look right!
you are using
Real ranage [ -.75 , -.74 ]
Imag range [ -.138 , -.75 ]
are you sure this is what you intended? It seems like an awfully stretched y-scale to me.
Also, standard mandelbrot algorithms tend to use
magnitude > 2
rather than 50.
as an escape check. Though this shouldn't affect the actual shape of the set.
BTW, there's no point in computing the sqrt of z*conj(z). Simply square the expressions on both sides of the inequality, giving if (z*conj(z) > 2500) and you've boosted the performance.