You are not logged in.

- Topics: Active | Unanswered

Yes, that is what happened when I was on M. Why is that happening?

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

This may be simplistic but you are obviously doing something wrong. Let's concentrate for a minute on my graph. What do you observe and how do you fix it?

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

I would subtract 0.3 frommy curve

*Last edited by Agnishom (2014-02-22 02:40:02)*

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

That suggests that you computed a[0], the constant term wrong.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

Hmm

```
(%i10) f(x) := if x>0 then sin(x) else 0$
(%i11) a(n) := (1/%pi)*(integrate(f(x)*cos(n*x),x,-%pi,%pi))$
(%i12) b(n) := (1/%pi)*(integrate(f(x)*sin(n*x),x,-%pi,%pi))$
(%i13) s(x,y) := a(0)/2 + sum((a(n)*cos(n*x)+b(n)*sin(n*x)),n,1,y)$
(%i14) plot2d([f(x),s(x,10)],[x,-%pi,%pi],[box, false]);
```

Why am I getting so many 'too many context' errors after that? Is it a bug?

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

I do not know very much about Maxima and its error codes.

I do know that you are not computing a[0] right.

`a0 = 1/(2 \[Pi]) Integrate[f[x], {x, -\[Pi], \[Pi]}]`

Replace a[0] with a0.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

Sorry, that is not possible right now.

f[x] is a piecewise function.

`f[x_] := If [x>0, Sin[x],0]`

How can M know what the following evaluates to without knowing if x is > 0?

```
a[0] + a[1] Cos[x] + a[2] Cos[2 x] + a[3] Cos[3 x] +
a[4] Cos[4 x] + a[5] Cos[5 x] + a[6] Cos[6 x] + a[7] Cos[7 x] +
b[0] + b[1] Sin[x] + b[2] Sin[2 x] + b[3] Sin[3 x] + b[4] Sin[4 x] +
b[5] Sin[5 x] + b[6] Sin[6 x] + b[7] Sin[7 x]
```

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

Hi;

That is not the correct way to enter the piecewise function.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

bobbym wrote:

Why didn't you tell me sooner?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

About the piecewise function? This was a question to you, not to me.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

Hm, how does a normal M user enter a piecewise function?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

`Piecewise[{{0, -\[Pi] <= x <= 0}, {Sin[x], \[Pi] >= x > 0}}]`

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

How is it different from mine?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

1) You asked how an M programmer would do it.

2) Yours defines the function passed π, mine does not.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

Agnishom wrote:

How can M know what the following evaluates to without knowing if x is > 0?

`a[0] + a[1] Cos[x] + a[2] Cos[2 x] + a[3] Cos[3 x] + a[4] Cos[4 x] + a[5] Cos[5 x] + a[6] Cos[6 x] + a[7] Cos[7 x] + b[0] + b[1] Sin[x] + b[2] Sin[2 x] + b[3] Sin[3 x] + b[4] Sin[4 x] + b[5] Sin[5 x] + b[6] Sin[6 x] + b[7] Sin[7 x]`

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

The piecewise function declares that x is sin(x) when x is between 0 and π.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

Yes, so what?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

The formulas produce an expression that will closely approximate the given function between -π and π.

Look at the answer. Notice how closely the blue line ( Fourier series ) covers the red line ( original function ).

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

That is not my question

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

I am not following you. Do you mean about the context error?

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

No, not that. I have now figured out the answer myself.

For now, I am integrating numerically.

```
(%i1) f(x) := if x>0 then sin(x) else 0$
(%i2) a(n) := (1/%pi)*(first(quad_qags(f(x)*cos(n*x),x,-%pi,%pi)))$
(%i3) b(n) := (1/%pi)*(first(quad_qags(f(x)*sin(n*x),x,-%pi,%pi)))$
(%i4) s(x,y) := a(0)/2 + sum((a(n)*cos(n*x)+b(n)*sin(n*x)),n,1,y)$
(%i5) plot2d([f(x),s(x,10)],[x,-%pi,%pi],[box, false]);
***MESSAGE FROM ROUTINE DQAGS IN LIBRARY SLATEC.
***INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED
* ABNORMAL RETURN
* ERROR NUMBER = 2
*
***END OF MESSAGE
***MESSAGE FROM ROUTINE DQAGS IN LIBRARY SLATEC.
***INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED
* ABNORMAL RETURN
* ERROR NUMBER = 2
*
***END OF MESSAGE
***MESSAGE FROM ROUTINE DQAGS IN LIBRARY SLATEC.
***INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED
* ABNORMAL RETURN
* ERROR NUMBER = 2
*
***END OF MESSAGE
***MESSAGE FROM ROUTINE DQAGS IN LIBRARY SLATEC.
***INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED
* ABNORMAL RETURN
* ERROR NUMBER = 2
*
***END OF MESSAGE
***MESSAGE FROM ROUTINE DQAGS IN LIBRARY SLATEC.
***INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED
* ABNORMAL RETURN
* ERROR NUMBER = 2
*
***END OF MESSAGE
***MESSAGE FROM ROUTINE DQAGS IN LIBRARY SLATEC.
***INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED
* ABNORMAL RETURN
* ERROR NUMBER = 2
*
***END OF MESSAGE
***MESSAGE FROM ROUTINE DQAGS IN LIBRARY SLATEC.
***INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED
* ABNORMAL RETURN
* ERROR NUMBER = 2
*
***END OF MESSAGE
***MESSAGE FROM ROUTINE DQAGS IN LIBRARY SLATEC.
***INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED
* ABNORMAL RETURN
* ERROR NUMBER = 2
*
***END OF MESSAGE
***MESSAGE FROM ROUTINE DQAGS IN LIBRARY SLATEC.
***INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED
* ABNORMAL RETURN
* ERROR NUMBER = 2
*
***END OF MESSAGE
***MESSAGE FROM ROUTINE DQAGS IN LIBRARY SLATEC.
***INFORMATIVE MESSAGE, PROG CONTINUES, TRACEBACK REQUESTED
* ABNORMAL RETURN
* ERROR NUMBER = 2
*
***END OF MESSAGE
(%o5)
```

Yields the attached image

The polynomial is the following

```
(%i6) s(x,10);
1.3877787807814457E-16 sin(10 x) 0.02020202020202 cos(10 x)
(%o6) -------------------------------- - --------------------------
%pi %pi
3.3219954564955856E-16 sin(9 x) 1.3877787807814457E-17 cos(9 x)
+ ------------------------------- - -------------------------------
%pi %pi
4.163336342344337E-17 sin(8 x) 0.031746031746032 cos(8 x)
+ ------------------------------ - --------------------------
%pi %pi
3.2265856653168612E-16 sin(7 x) 5.7245874707234634E-17 cos(7 x)
- ------------------------------- - -------------------------------
%pi %pi
3.9091435841719085E-16 sin(6 x) 0.057142857142857 cos(6 x)
- ------------------------------- - --------------------------
%pi %pi
4.8572257327350599E-17 sin(5 x) 1.6653345369377348E-16 cos(5 x)
- ------------------------------- + -------------------------------
%pi %pi
2.0816681711721685E-16 sin(4 x) 0.13333333333333 cos(4 x)
- ------------------------------- - -------------------------
%pi %pi
3.2612801348363973E-16 sin(3 x) 1.9081958235744878E-17 cos(3 x)
+ ------------------------------- + -------------------------------
%pi %pi
1.6653345369377348E-16 sin(2 x) 0.66666666666667 cos(2 x)
- ------------------------------- - -------------------------
%pi %pi
1.570796326794897 sin(x) 1.1102230246251565E-16 cos(x) 1.0
+ ------------------------ - ----------------------------- + ---
%pi %pi %pi
```

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

There are a lot of error messages in that calculation. Also, many of those coefficients are just fungi.

But that is the correct answer. Very good.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

fungi?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 102,328

A number that is very, very tiny so it is obviously 0.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**

Offline

All tiny numbers are essentially zero? Why?

There are a lot of error messages in that calculation.

Not the calculation, its just the plotting part.

*Last edited by Agnishom (2014-02-22 15:50:39)*

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline