DuncanA

703 Reputation

8 Badges

18 years, 189 days

MaplePrimes Activity


These are answers submitted by DuncanA

In the 5th line, matrix L is indexed by a float 1.1. It should be

U[1, 2] := A[1, 2]/L[1, 1];

Additionally, the second of the two return statements in the procedure will not execute because the procedure will terminate at the first return statement. Using RETURN is unnecessary. The final two lines could be replaced with

eval(L), eval(U);

Another point to note is that the 'matrix' data type used is deprecated. The newer 'Matrix' data type is preferred.

---

Duncan

The assignment operator in Maple is := so in your for loop

for i from 1 to M do x[i]=evalf(a* cos(t[i])) end do;

you must add a colon (:) before the equals sign 

for i from 1 to M do x[i]:=evalf(a* cos(t[i])) end do;

---

Duncan

One alternative is to use the operator form of `if` inside the addition with `add` replacing `sum` because we are not performing a symbolic summation.

expr := k^2  +  j:

add(add(`if`(15 >= expr and expr >= 3, k+j, 0), k=0..5), j=1..8);

                                     166

---

Duncan

Correct results are not displayed because of a flaw in the method used. The solution is being overwritten whenever X[k] > 0 is true. To prevent this an additional test condition (Y[k]/X[k] < solution) must be added to the if statement.

Y := [24,6,1,2]:
X := [6,1,-1,0]:
solution := infinity:
for k from 1 to 4 do
    if X[k] > 0 and Y[k]/X[k] < solution then
        solution := Y[k]/X[k];
    fi;
od;
solution;
                                      4

An alternative method is to form a third list Z and use Maple's builtin min function to find the correct result.

Z := [seq(`if`(X[i]>0, Y[i]/X[i], infinity), i=1..4)];
                         [4, 6, infinity, infinity]
min(Z);
                                      4

 

---

Duncan

 

There are a number of problems with the interpol procedure as currently defined.  

showstat(interpol);
  13   p1(x) = f(x0)+f[x0,x1](x-x0);          
# line 13 has no effect.  There is no assignment `:=`, x0, x1, and p1 are not defined.  f[x0,x1](x-x0) ?
  23         y[i] = (y[i]-y[i-1])/(x[i]-x[i-j])
# line 23 has no effect.  `=` should be `:=`.
  25   p[k] = y[1];
# line 25 has no effect.  Again, `=` should be `:=`.
---

Duncan

If you type 

diff(f(x),x); 

in either 1-D Math mode or 2-D Math mode, select what you've just typed and convert to 2-D Math (either "Format>Convert To>2-D Math Input" or "right-click>Convert To>2-D Math Input") then diff(f(x),x); will be converted to 

diff(f(x),x)

When this is evaluated, its value will be

2x 

if f(x) has been declared beforehand as

f:=x->x^2;

 

---

Duncan

The CodeTools package  ?CodeTools  "contains subpackages and commands to help improve the efficiency and quality of Maple code." The CodeTools package also contains the Profiling subpackage which contains "commands to perform statement level profiling" so this may be of use to you.

---

Duncan

The garbage collector can be invoked by calling gc(); in your code. This will make more memory available by deleting data that is no longer being referenced.

kernelopts(gcbytesavail); and kernelopts(gcbytesreturned); provide information on the number of bytes available after the last garbage collection and the number of bytes returned by the last garbage collection.

Uploading a copy of your worksheet may allow others to suggest a way to make the calculation more efficient.

---

Duncan

My (educated?) guess is that the enthalpy of the reaction can be found by equating the sum of the reactants with the sum of the products and solving to find the enthalpy change (represented by dH, below).

http://www.ausetute.com.au/enthchan.html

 

 

> restart;
> CaO := -194.05*Unit('kJ'/'mol'):
> B2O3 := -137.49*Unit('kJ'/'mol'):
> B2O32CaO := -210.40*Unit('kJ'/'mol'):
> reaction := 2*CaO+B2O3 = B2O32CaO+dH;
                                         /     2        \   
                            5            |('m')  ('kg') |   
               -5.2559000 10  Units:-Unit|--------------| =
                                         |     2        |   
                                         \('s')  ('mol')/   
                                         /     2        \     
                            5            |('m')  ('kg') |     
               -2.1040000 10  Units:-Unit|--------------| + dH
                                         |     2        |     
                                         \('s')  ('mol')/     
> dH := solve(reaction, dH);
                                          /     2        \
                             5            |('m')  ('kg') |
                  -3.15190 10  Units:-Unit|--------------|
                                          |     2        |
                                          \('s')  ('mol')/



---

Duncan

Maple has the assume facility, ?assume 

> assume(a, complex);
> about(a);
Originally a, renamed a~:
  is assumed to be: complex

---

Duncan

Convert the table to a list and plot the list

t := table():
t[a] := 12:
t[th] := 15:
t[ch] := 14:
t[l] := 12:
t[k] := 4:
t[ck] := 22:
plots:-listplot(convert(t, list));

---

Duncan

Ignoring ties, here is a simulation of the War game.  It starts with a deck of 52 'cards' numbered 2 .. 14 (2..10,J,Q,K,A) in four suits.  The deck is shuffled and two hands are dealt.  Each hand is placed into a queue.  One 'card' is removed from each queue and a comparison is made.  The winning player adds both cards to his queue.  The game continues until one queue is empty when one player will be declared the winner.  The code below can be modified to cope with ties.

 

restart;
with(combinat):
with(queue):
deck := [seq(seq(i, i = 2 .. 14), j = 1 .. 4)];
nops(deck);
shuffle := randperm(deck);
hand['a'] := [seq(shuffle[i], i = 1 .. nops(deck), 2)];
hand['b'] := [seq(shuffle[i], i = 2 .. nops(deck), 2)];
Qa := new(op(hand['a']));
Qb := new(op(hand['b']));
length(Qa), length(Qb);
while not empty(Qa) and not empty(Qb) do
  a := dequeue(Qa);
  b := dequeue(Qb);
  if a > b then
    print(a, b, `'a' wins battle`);
    enqueue(Qa, a);
    enqueue(Qa, b);
  elif a < b then
    print(a, b, `'b' wins battle`);
    enqueue(Qb, b);
    enqueue(Qb, a);
  else
    # tie
  end if;
  print(length(Qa) , length(Qb));
end do:
if empty(Qb) then
  print(`'a' conquers 'b'`)
elif empty(Qa) then
  print(`'b' conquers 'a'`)
end if:

 

---

Duncan

?Task/MatrixInverse 

---

Duncan

 

sol := isolve({x - 565 = 5656 * r, x - 56 = 565 * s},{t});
    sol := {r = 386 + 565 t, s = 3865 + 5656 t, x = 2183781 + 3195640 t}
eval(sol, t=1);
                      {r = 951, s = 9521, x = 5379421}
5379421 mod 5656 = 565 ; 5379421 mod 565 = 56;
                                  565 = 565
                                   56 = 56

 

---

Duncan

Introduce a local variable and move the initial N:=N+1; to after the while loop and calls to 'cat'.

NewName := proc ()
    global N; local t;
    while assigned(cat(C, N)) do
        N := N+1;
    end do;
    t := cat(C, N);
    N := N+1;
    t
end proc;


 

---

Duncan

2 3 4 5 6 7 Page 4 of 7