#1
by ととさん » 6年前
大学の課題で
x = t cos t, y = t sin t (0 ≤ t ≤ 6π)のグラフを作成したいのですが
以下のようにfor文を書いてみたものの全く作成されません。
どの様に改善したらよいでしょうか。
※正解は渦巻きのようなグラフになります。
[date]
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
FILE *fp;
double sin(x),cos(x);
if ((fp = fopen("graph.csv", "w")) == NULL){ /* ファイルを開く */
/* 開けなかった場合の処理 */
fprintf(stderr, "Cannot open 'graph.csv'\n");
exit(1);
}
for(cos(x)=0; cos(x) <=18.8495559; cos(x)+=0.01)
fprintf(fp,"%f,%f\n",cos(x),sqrt(1-x*x)+sqrt(fabs(x)));
for(sin(x)=0; sin(x) >=-18.8495559; cos(x)-=0.01)
fprintf(fp,"%f,%f\n",cos(x),-sqrt(1-x*x)+sqrt(fabs(x)));
fprintf(fp,"-1,1\n");
fclose(fp); /* ファイルを閉じる */
return 0;
}
[/date]
大学の課題で
x = t cos t, y = t sin t (0 ≤ t ≤ 6π)のグラフを作成したいのですが
以下のようにfor文を書いてみたものの全く作成されません。
どの様に改善したらよいでしょうか。
※正解は渦巻きのようなグラフになります。
[date]
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
FILE *fp;
double sin(x),cos(x);
if ((fp = fopen("graph.csv", "w")) == NULL){ /* ファイルを開く */
/* 開けなかった場合の処理 */
fprintf(stderr, "Cannot open 'graph.csv'\n");
exit(1);
}
for(cos(x)=0; cos(x) <=18.8495559; cos(x)+=0.01)
fprintf(fp,"%f,%f\n",cos(x),sqrt(1-x*x)+sqrt(fabs(x)));
for(sin(x)=0; sin(x) >=-18.8495559; cos(x)-=0.01)
fprintf(fp,"%f,%f\n",cos(x),-sqrt(1-x*x)+sqrt(fabs(x)));
fprintf(fp,"-1,1\n");
fclose(fp); /* ファイルを閉じる */
return 0;
}
[/date]