## 5057 Reputation

14 years, 60 days

"A map that tried to pin down a sheep trail was just credible,

but it was an optimistic map that tried to fix a the path made by the wind,

or a path made across the grass by the shadow of flying birds."

- _A Walk through H_, Peter Greenaway

## operator ans...

Did you mean to pass ans instead of ans(lambda[0],TT) as the first argument to NonlinearFit?

Would it be safer to use a form like solL(parameters=[lambda[0]=p1,T[m]=p2]) inside ans, to keep it straight which is which?

## structure...

The reason it's hard for you to notice mistakes is the lack of structure in the worksheet. The Export() calls are inside the wrong loop (one too many inwards). You aren't resetting G to the empty list each time angle1 changes. There are too many calls to with(). Etc.

Start by giving your worksheet some structure.

Indent all your for-do-loops, using nested indentation where necessary.

Place all those calls to with() at the very start of the sheet. Do them just once.

You should ideally get rid of G altogether, as augmenting G each time through the loop makes it much slower than it has to be. But if you really want to keep using G, then make sure you reassign G:=[] at the start of the angle1 loop.

Check whether your simple version above could be written something roughly like the following.

`restart:with(LinearAlgebra):  with(ExcelTools):L := Matrix([["j", "i", "c", "b"]]);M := Matrix(4,4):for j from 1 to 10 do;  for i from 1 to 4 do;    o := 1;    c := j*i;    b := 2*c;    M[i, 1..4] := Vector[row]([j, i, c, b]);  end do;    Export(L, sprintf("C:/TEMP/%d_%d.xls", j, o), "Sheet0", "A1");    Export(M, sprintf("C:/TEMP/%d_%d.xls", j, o), "Sheet0", "A2");end do;`

I didn't indent, but this might be more what you want. (I changed the filenames in a minor way, but they still depend on the variable values.)

oneslopeloopage_ed.mw

I changed lowerbound and upperbound to lowerbd and upperbd. I left most of the layout unchanged, and didn't fix several of the efficiency problems.

## fsolve...

```G:= X->S.LinearAlgebra:-LinearSolve(evalf(subs(x=X,R)),T)-L:

fsolve(G); # or fsolve(G, a..b) if you know a candidate range for numeric `a` and `b`
```

## reference...

Try this method due to Joe Riel.

Someone else asked a similar question recently, but there seem to be as yet unresolved issues.

## fprintf...

Perhaps you could use the fprintf command, and only fclose the file when you've finished appending it with Matrices.

The flavour of the call to fprintf might depend on what kind of data you have, how you want entries delimited, etc. You may be able to get it to match the behaviour of whatever options you're using for ExportMatrix.

Here's a float example, writing out the Matrices with tab-delimited entries. With only a single line-break being written (another choice) after the written Matrices, the resulting file can even be imported a a single larger set of data. You can add more \n end-of-lines between them if you'd like. Several other aspects are controllable.

```> restart:

> M:=LinearAlgebra:-RandomMatrix(2,generator=-100.0..100.0);

[82.6751712278038724     81.1583874151238547]
M := [                                           ]
[-74.6026367412987952    62.9447372786357846]

> N:=LinearAlgebra:-RandomMatrix(2,generator=-100.0..100.0);

[9.37630384099676917     -80.4919190001180880]
N := [                                            ]
[-44.3003562265903241    26.4718492450818985 ]

> fopen("mm.txt",WRITE):

> fprintf("mm.txt", "%{c\t}.15f\n", M):

> fprintf("mm.txt", "%{c\t}.15f\n", N):

> fclose("mm.txt"):

> ImportMatrix("mm.txt");
[82.675171227803872     81.158387415123855 ]
[                                          ]
[-74.602636741298795    62.944737278635785 ]
[                                          ]
[ 9.376303840996769     -80.491919000118088]
[                                          ]
[-44.300356226590324    26.471849245081898 ]
```

Are you using some special options in your ExportMatrix call? (It might get a little more involved if you are are exporting to a Matlab binary format.)

## in order to call what?...

What do you want to access from other worksheets?

Do you want to be able to access previously computed results, or to be able to call procedures defined in those worksheets, or something else? You probably need to answer this clearly, to get an answer most useful to you.

Do you want to be able to do it only if the other worksheets are currently opened in the same GUI session? If that is the case then you can open them all -- primary and others -- with the very same kernel (Math engine). You can do that by changing your Maple configuration using Tools / Options /General and changing the drop-down menu box titled "How should Maple handle the creation of new Math engines?". Change it to "Ask each time a new document is created". After that, you'll get prompted as to which kernel you want t use, each time you open multiple documents within a single Standard GUI session. By choosing the same kernel for multiple concurrently open Worksheets or Documents they can all share assigned values such as numeric values for variables, expressions, procedures, etc. But they only share what's been computed in that session (excepting possibly large amounts of numeric data that the GUI sometimes prompts you to store with the sheet).

Or, do you just want to save defined procedures, data, expressions, etc, and re-use them in any new worksheet, without having to open the sheets in which those results were computed? If that's your goal, then you could save the prior results to a Library archive. See here and here for some previous notes on that kind of thing. Here is a related note.

## fprintf...

You could use fprintf to write to the file, and make the CodeGeneration:-Matlab command return a string to be so printed.

`> restart;> fprintf("testfile2.txt","%s\n",CodeGeneration:-Matlab(u(1) = 3, 'output'=string));Warning, the function names {u} are not recognized in the target language                               17> fclose("testfile2.txt"); # when finished all appends to the file> fprintf("testfile3.txt","%s\n",CodeGeneration:-Matlab(eval(`sin/normal`), 'output'=string));Warning, the function names {ASSERT, `tools/sign`} are not recognized in the target language                              298> fclose("testfile3.txt"); # when finished all appends to the file`

## multiplication...

You've likely entered `xy` as if it were a single name.

Try placing an explicit multiplication symbol (typed as *) between the x and y in the denominator.

Or if you're using 2D Math input then you could include a space (to denote implicit multiplication) between that x and y in the denominator.

Some people have found that the habit of always using explicit multiplication symbols (* instead of space) helps avoid such problems.

## evalM...

You should be able to use Maple's evalM command for this. (note the mixed capitalization of that command.)

The expressions in Matlab syntax that evalM supports includes function calls. You can even call it several times back-to-back in order to set the working directory, assign variables, then call a matlab function with them, etc. (You might have to set the working directory in order for matlab to pick up some function you might have in a .m file, etc.)

Here's someone doing all that.

## two ways...

Here are two ways. (I didn't bother assigning the results to anything, but you could.)

```> restart:

> for j from 1 to 3 do
>   unapply(x+y^j,[x,y]);
> end do;

(x, y) -> x + y
2
(x, y) -> x + y
3
(x, y) -> x + y

> restart:

> F:=proc(x,y) option arrow, operator; x+y^J; end proc:

> for j from 1 to 3 do
>   subs(J=j,eval(F));
> end do;

(x, y) -> x + y
2
(x, y) -> x + y
3
(x, y) -> x + y
```

## event...

```> restart:

> s := dsolve({diff(x(t),t)=x(t), x(0)=0.5}, x(t), numeric,
>             events=[[x(t)=0..1, halt]]):

> s(0.4);
[t = 0.4, x(t) = 0.745912305379683]

> s(2.0);
Warning, cannot evaluate the solution further right of .69314729, event #1 triggered a halt
[t = 0.693147299975523, x(t) = 1.]
```

## LinearAlgebra vs linalg...

Are you inadvertantly mixing the LinearAlgebra and linalg packages? If so, then just consider sticking to LinearAlgebra.

Are you trying something like this, using linalg[exponential]?

```with(LinearAlgebra):with(linalg):
A:=([[-5,1],[-5,0]]);
a:=exponential(A,1);
MatrixPower(a,k);
```

If so, then you could try instead

```with(LinearAlgebra):
A:=Matrix([[-5,1],[-5,0]]);
a:=MatrixExponential(A,1);
MatrixPower(a,k);
```

But, if so, then how about just joining those last two commands into MatrixExponential(A,k)? It gives a much simpler and compact form for the result.

```restart:
with(LinearAlgebra):
A:=Matrix([[-5,1],[-5,0]]):
a:=MatrixExponential(A,1):
G1:=MatrixPower(a,k):
G2:=MatrixExponential(A,k):
[0  0]
[    ]
[0  0]
```

That last command, which checks those two results as being the same, is similar to

```simplify(map(radnormal,map(expand,G1-G2)));
```

## GAMMA...

```> sum(k*p*convert(Beta(k,p+1),GAMMA), k = 1 .. infinity) assuming p>1;

p
-----
p - 1
```

## quirks...

For your first example, you could try factor(expand(expr2)) where expr2 is your second form of the expression.

For your second example, there are some automatic simplifications and expansions which Maple applies. This particular one is affected by whether there are other factors in the product term.

```expr:=2*(1+x)*y;
2 (1 + x) y

expand(expr);
2 y + 2 y x

factor(%);
2 (1 + x) y

expr:=2*(1+x); # automatic
2 + 2 x

expand(expr);
2 + 2 x

factor(%); # even if it worked, you'd get the automatic expansion right away!
2 + 2 x
```

(See here for a recent post on this theme, in which you can see a few workaround suggestions.)

## solve and eliminate...

[edited: rolling delta1=delta2 into a single name, dd]

```restart:

values:={a1=17.7122,b1= 10.2527,B12= 47.3210,G11= 30.7858,
P1=52.2526,D1=1,a2=14.6054,b2= 10.1402,B21=33.8007,
G22=19.0708,P2=48.4902,D2= .2}:

eq1:=D1*w1 = P1-G11*E1^2-B12*E1*E2*sin(delta1-delta2);
eq2:=a1*E1 = b1*E2*cos(delta1-delta2)+u1;
eq3:=D2*w2 = P2-G22*E2^2+B21*E1*E2*sin(delta1-delta2);
eq4:=a2*E2 = b2*E1*cos(delta1-delta2)+u2;

S:=eliminate(eval(algsubs(delta1-delta2=dd,{eq1,eq2,eq3,eq4}),
values),{E1, E2});

sol:=solve(op(2,S),{w1,w2});
```

I too don't see how to eliminate dd=delta1-delta2 nicely. If you want you can impose a value (here, zero) on dd=delta1-delta2

```eval(sol,dd=0);
```
 First 13 14 15 16 17 18 19 Last Page 15 of 48
﻿