怎么写程序?

时间:2015-04-24 17:12:57

标签: wolfram-mathematica procedural-programming

您好!请帮助我从我的程序代码中程序。 我有代码,包括2个部分(它们是图形[...]),它用rectengles绘制函数。 但它似乎是太多的代码,所以我必须编写一个带有函数参数的过程,然后当我调用它时,这个过程也会这样做(用rectengles绘制函数)。 (有些人可以使用spx1,spx2。)

这是完整的代码:

        (*Sin[x]*)
data1 = Table[{x, Sin[x]}, {x, -1, 6, 0.1}];
    data11 = data1[[;; , 2]];(*отримуємо набір ординат*)localmins1 = 
     Pick[data1, MinDetect[data11, 10^-6], 1];
    localmaxs1 = Pick[data1, MaxDetect[data11, 10^-6], 1];
    Graphics[{Thick, Blue, Line[data1], Red, PointSize[0.01], 
       Point[localmins1], Point[localmaxs1]}, Axes -> True];       

spx1 = {-1, -0.35, 0.3, 0.95, 1.6, 2.375, 3.15, 3.925, 4.7, 6};
    Graphics[{Thick, Blue, Line[data1], Red, PointSize[0.01], 
      Point[localmins1], Point[localmaxs1], 
      Point[{{-1, 0}, {1.6, 0}, {-0.35, 0}, {0.3, 0}, {0.95, 0}}], Green, 
      Point[{{-0.35, -0.1342898}, {0.3, 0.29552}, {0.95, 0.813416}}], Red,
       Point[{{2.375, 0}, {3.15, 0}, {3.925, 0}, {4.7, 0}}], Green, 
      Point[{{2.375, 0.693685}, {3.15, -0.0084}, {3.925, -0.70569766}}], 
      Pink, Opacity[.7], EdgeForm[Directive[Dashed, Pink]], 
      Rectangle[{-1, -0.84147}, {-0.35, -0.342898}], 
      Rectangle[{-0.35, -0.342898}, {0.3, 0.29552}], 
      Rectangle[{0.3, 0.29552}, {0.95, 0.813416}], 
      Rectangle[{0.95, 0.813416}, {1.6`, 0.9995736030415051`}], 
      Rectangle[{1.6, 0.99957}, {2.375, 0.693685}], 
      Rectangle[{2.375, 0.693685}, {3.15, -0.0084}], 
      Rectangle[{3.15, -0.0084}, {3.925, -0.70569766}], 
      Rectangle[{3.925, -0.70569766}, {4.7, -0.9999}],}, Axes -> True]

    (*Cos[x]*)
    data2 = Table[{x, Cos[x]}, {x, -3, 4, 0.1}]; data22 = 
     data2[[;; , 2]];(*отримуємо набір ординат*)localmins2 = 
     Pick[data2, MinDetect[data22, 10^-6], 1];
    localmaxs2 = Pick[data2, MaxDetect[data22, 10^-6], 1];
    Graphics[{Thick, Blue, Line[data2], Red, PointSize[0.01], 
       Point[localmins2], Point[localmaxs2]}, Axes -> True];

    spx2 = {-3,-2.25, -1.5, -0.75,0, 0.75, 1.5, 2.25, 3};
    spxrozb11 = {-3, -2.25, -1.5, -0.75, 0};
    spxrozb22 = {0, 0.75, 1.5, 2.25, 3};
    Graphics[{Thick, Blue, Line[data2], Red, PointSize[0.01], 
      Point[localmins2], Point[localmaxs2], 
      Point[{{-2.25, 0}, {-1.5, 0}, {-0.75, 0}, {0, 0}}], Green, 
      Point[{{-2.25, -0.628}, {-1.5, 0.07}, {-0.75, 0.73}}], Red, 
      Point[{{0.75, 0}, {1.5, 0}, {2.25, 0}, {3, 0}}], Green, 
      Point[{{0.75, 0.7316}, {1.5, 0.07}, {2.25, -0.628}}], Pink, 
      Opacity[.7], EdgeForm[Directive[Dashed, Pink]], 
      Rectangle[{-3, -0.989992}, {-2.25, -0.628}], 
      Rectangle[{-2.25, -0.628}, {-1.5, 0.07}], 
      Rectangle[{-1.5, 0.07}, {-0.75, 0.7316}], 
      Rectangle[{-0.75, 0.7316}, {1.6653345369377348`*^-16, 1.`}], 
      Rectangle[{1.6653345369377348`*^-16, 1.`}, {0.75, 0.7316}], 
      Rectangle[{0.75, 0.7316}, {1.5, 0.07}], 
      Rectangle[{1.5, 0.07}, {2.25, -0.628}], 
      Rectangle[{2.25, -0.628}, {3.1000000000000005`, \
    -0.9991351502732795`}]}, Axes -> True]

我试着写出程序的第一步,即使它不起作用;(

f1 = Table[{x, Sin[x]}, {x, -1, 6, 0.1}];
f2 = Table[{x, Cos[x]}, {x, -3, 4, 0.1}];
data = {f1, f2};
spx1 = {-1, -0.35, 0.3, 0.95, 1.6, 2.375, 3.15, 3.925, 4.7, 6};
graph[i_] := 
 Graphics[For[i = 0, i < 3, 
   i++, {Thick, Blue, Line[data[[i]]], Red, Point[{spx1[[i]], 0}]}]]
graph[1]

******如何正确编写程序中的For?******

0 个答案:

没有答案