佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 841|回复: 9

请问这提要如何做,我有做功课了的,只是还是有问题,请大家帮忙,谢谢

[复制链接]
发表于 20-4-2006 09:57 PM | 显示全部楼层 |阅读模式
Given a function , and an interval  , write a function each to perform the following tasks:

(i)        Plot   against   for the given interval.
(ii)        Determine the minimum point of   in the given interval.

The main body of the program "project.c" and a header file "project.h" containing the definition of the function   and the interval   are supplied.

Hint: There are a few techniques to find the minimum point of a function. Some examples are steepest descent method and Newton method.



我的 VC++ code :

[project.h] 内的:

#define XMIN -10
#define XMAX 20
double A;
double f(double x)

{
   return A = pow(x - 3,2)+4;
}

============================================================================================

[project.c] 内的:

#include <stdio.h>
#include <math.h>
#include "project.h"

#define XSIZE 64

double findmin(); /* You may define your functions to accept parameters, if necessary */
void drawgraph();

int main()
{
    drawgraph();
    findmin();

    return 0;
}

=============================================================================================


[INPROV.CPP] 内的:


#include <stdio.h>
#include <math.h>
#include"project.h"

#define XSIZE 64
#define YSIZE 42

double findmin (double value[XSIZE]),lower;
void drawgraph (double value[XSIZE]);

int main ()
{
        int i, j;
        double temp= XMIN, XINT;

        double man [XSIZE];
        double man2 [XSIZE];

                XINT=(double)(XMAX-XMIN)/XSIZE;
               
                        for (i=0; i<XSIZE; i++)

                        {
                                man = temp;
                                temp= temp +XINT;
                        }

                        for (j=0; j<XSIZE; j++)
                        {
                                man2[j]=f(man [j]);
                        }

                drawgraph(man2);

                printf("        %d",XMIN);

                        for(i=0;i<=XSIZE-1;i++)

                        {
                                printf(" ");
                        }

                        printf("%d",XMAX);

                        printf ("\n\n");

        return 0;
}

double findmin (double value[XSIZE])
{
        int k;
        double temp=value[0];

        for (k=1; k<XSIZE; k++)
        {

                if (value[k]<temp)

                        temp=value[k];

        }

        return temp;
}

double findmax(double value[XSIZE])
{

        int k;

        double temp=value[0];

        for(k=1; k<XSIZE; k++)

        {

                if (value [k]>temp)

                        temp=value[k];

        }

        return temp;
}

void drawgraph(double value[XSIZE])
{

        int i,j,miny;

        double min, k, YMAX, YMIN, YINT, test2, test3[YSIZE], minx;


        char graph[YSIZE][XSIZE];

        YMAX=findmax(value);

        YMIN=findmin(value);

        k=YMAX;

        YINT=(double)(YMAX-YMIN)/(YSIZE-1);


        for(i=0; i<YSIZE; i++)

        {

                test3 =k;

                k-=YINT;

        }


        for(i=0; i<YSIZE; i++)

                for(j=0; j<XSIZE; j++)

                        graph [j]= '.';


                for(i=0; i<XSIZE; i++)

                        graph [YSIZE-1]= '-';


                for(j=0; j<XSIZE; j++)

                {

                        test2 = value[j]/YINT;

                        test2= YSIZE-test2;

                        graph[(int)test2][j]='*';

                }



                min=findmin (value);

                for (i=0; i<XSIZE; i++)

                        if (value==min)

                        {

                                miny=i;

                                break;

                        }

                        minx= min/YINT;

                        minx= YSIZE-minx;

                        graph [(int)minx][miny] ='M';


                        for (i=0; i<YSIZE; i++)

                        {

                                if (test3/10 <1)

                                        printf ("   %f",test3);

                                else if (test3/100 <1)

                                        printf("  %f",test3);

                                else if (test3/1000 <1)

                                        printf (" %f", test3);

                                else

                                        printf("%f", test3);


                                for (j=0; j<XSIZE; j++)

                                        printf ("%c",graph[j]);

                                printf ("\n");

                        }

}



=============================================================================================


*** 注意,在还没有 run 这个 INPROV.CPP 之前,请大家把 project.h & project.c 这两个 file 放入大家的 VC++ 的 installation directory 内 ( program files > microsoft visual studio > VC98 > Include 内), 不然的话是会有 error 的。。。。

好的我现在就讲下我面对的问题: 其实这个 program 是要在一个 function 的 interval 内找到 X minimum 的 value。。。 请大家打开刚刚你的放入 Include folder 内的 project.h file 看看,里面含有一个 power of 2 的 function,在这个情况下, 我在 execute INPROV.CPP 后, 我会看见以下的图:



但是, 如果我将 project.h 内的 function 换成是 power of 3 的 function [ eg: pow(x-3,3)+4 ] 的话, 那个 program execute 出来的 output 还是跟 power of 2 的 graph 一样。。。意思就是说错了。。。为什么呢,,,我想了很久还是没有头绪。。。我还是初学者而已,请各位大大帮忙,谢谢。



>>>以上的 3 个 files <<<

[ 本帖最后由 funky 于 20-4-2006 10:05 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 21-4-2006 06:57 PM | 显示全部楼层
没有人看得懂吗??? = =

其实对我来说也是有点儿眼花 @.@
回复

使用道具 举报

发表于 21-4-2006 09:57 PM | 显示全部楼层

回复 #2 funky 的帖子

为何你的prgoram 有error? graph[YSIZE-1]= '-'; graph is 2 dimensional array, 不应能这样assign ....
回复

使用道具 举报

 楼主| 发表于 21-4-2006 10:41 PM | 显示全部楼层
原帖由 tanhy 于 21-4-2006 09:57 PM 发表
为何你的prgoram 有error? graph[YSIZE-1]= '-'; graph is 2 dimensional array, 不应能这样assign ....


怎样说呢?之前也是有人说过, 但是我还是不明白什么意思, 这个我的老师没有教过我 

他在算着 pow of 2 的 function 就没有问题,但是用来算 pow of 3 的 function 就有问题了, 出来的图和 pow of 2 的 function 的 graph 一样。。。
回复

使用道具 举报

发表于 22-4-2006 07:58 AM | 显示全部楼层

回复 #4 funky 的帖子

你可以:graph[i][j] = '-', 或strcpy(graph[i],"-"), graph[i] 是一个string, 不能assign char
你把bug fix 了才post 上来吧
回复

使用道具 举报

发表于 22-4-2006 08:53 AM | 显示全部楼层
楼主的bug不需要fix的。。。

因为,下载连接那里的代码是对的。。在1楼列出来的跟下载那里的是不一样的。。
楼主应该把东西整理好才post给大家。。。
下载那里的代码,我run过,没问题。改成pow(x-3,3)+4 ,也显示会显示power of 3的graph。

这个不是微积分(calculus)的东西吧?为何用programming作这么复杂的东西?
如果我没弄错,楼主的解法好像不大对。。。

题目要的minimum point而不是minimum value。还有steepest descent应该是需要找到曲线的斜度,才能找到minima和maxima。

如果我有说错的话,请大家改正。
回复

使用道具 举报

Follow Us
 楼主| 发表于 3-5-2006 09:02 PM | 显示全部楼层

回复 #6 meemee 的帖子

对对对。。。。那个死人 lecturer 昨天才跟我们说要这样, 之前又没有讲。。。。现在要从做, 我真得很不明白那个题目咧。。。。有谁可以指点指点。。。。please。。。我的头很大。。。。

还有, 他说我的 code 错了哦。。。。又没有讲要怎样改。。。惨了~
回复

使用道具 举报

发表于 4-5-2006 12:56 PM | 显示全部楼层
原帖由 funky 于 3-5-2006 09:02 PM 发表
对对对。。。。那个死人 lecturer 昨天才跟我们说要这样, 之前又没有讲。。。。现在要从做, 我真得很不明白那个题目咧。。。。有谁可以指点指点。。。。please。。。我的头很大。。。。

还有, 他说我的 cod ...


呵呵。。。你的代码里面的[ i ]会不见,因为这个你输入时会把[ i ]以后的字转换成italian word。它当成[ i ] [ /i ]所以你一些字体会变成斜体字。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 4-5-2006 05:45 PM | 显示全部楼层
原帖由 qiyan 于 4-5-2006 12:56 PM 发表


呵呵。。。你的代码里面的[ i ]会不见,因为这个你输入时会把[ i ]以后的字转换成italian word。它当成[ i ] [ /i ]所以你一些字体会变成斜体字。


那么和我的问题有什么关系呢???

其实我的那个死人 lecturer 叫我重做过, 他说只要做一个 .c file ,从一个 function f(x) 内找出 他的 minimum value.....

要用 steepest decent 来找。。。。。看到这里, 我就头痛了。。。
回复

使用道具 举报

发表于 4-5-2006 07:47 PM | 显示全部楼层
原帖由 funky 于 4-5-2006 05:45 PM 发表


那么和我的问题有什么关系呢???

其实我的那个死人 lecturer 叫我重做过, 他说只要做一个 .c file ,从一个 function f(x) 内找出 他的 minimum value.....

要用 steepest decent 来找。。。。。看到 ...


因为你上面post的代码和你给的link里面的代码不一样,因为有些地方有[ i ]而你post的代码却没有[ i ],都被这个forum吃掉了,这里有的人看不明白,所以会有人说应该用2 dimension arrays及compile有error。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-9-2024 01:28 PM , Processed in 0.098281 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表