Message Boards Message Boards

0
|
5606 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to fix a procedure?

Help me, please! There's the procedure operation[f_]. It works correctly and plot for functions:Cos,Sin. But, Unfortunately, it doesn't work for E^x and Log[E,x] and outputs errors, maybe because inputting not correct name of function or something else;(( What's the problem?

spxsin = {-1, -0.35, 0.3, 0.95, 1.6, 2.375, 3.15, 3.925, 4.7, 5.025, 
5.35, 5.675, 6};
spxcos = {-1, -0.75, -0.5, -0.25, 0, 0.775, 1.55, 2.325, 3.1, 3.825, 
4.55, 5.275, 6};
spxlny = {-1, 0.75, 2.5, 4.25, 6};
spxey = {-1, 0.75, 2.5, 4.25, 6};
operation[f_] := Block[{data},
data = Table[{x, f[x]}, {x, -1, 6, 0.1}];
Graphics[{Thick, Blue, Line[data],
Green, Table[Point[{spx[­[i]], f[spx[­[i]]]}], {i, 1, Length[spx]}],
Pink, Opacity[.7], 
Table[Rectangle[{spx[­[i]], f[spx[­[i]]]}, {spx[­[i + 1]], 
f[spx[­[i + 1]]]}], {i, 1, Length[spx] - 1}]
}, Axes -> True]]

Which[ f == Sin, spx := spxsin, f == Cos, spx := spxcos, f == E^x , 
spx := spxlny, f == Log, spx := spxey]

operation[Sin]
operation[Cos]
operation[E^x]
operation[Log]
POSTED BY: Julia Ilkiv
3 Replies
Posted 9 years ago
  1. The mathematica function for the exponential is Exp[]. 1.1. Evaluating E^x//FullForm should show you that the head of this expression is not Exp but Power 1.2. Defining the exponential function in a similar fashion like you did, would require something like (E^#)& (probably, I haven't tested it)

  2. The problem you are having with Log[] is because the logarithm is not defined for negative input values while your spxey[[1]]==-1

hope that helps

POSTED BY: nik tsak

Thanks! Exp works! but unfortunately, Log doesn't work, though I edited spxey = {0.75, 2.5, 4.25, 6}; and data = Table[{x, f[x]}, {x, 1, 6, 0.1}]; Can You help me?

POSTED BY: Julia Ilkiv
Posted 9 years ago

What exactly is wrong because I can't see any prob. It evaluates just fine for me.

Perhaps you could explain what you were expecting to receive as output and what you actually get?

POSTED BY: nik tsak
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract