Fabio92

97 Reputation

7 Badges

7 years, 126 days

MaplePrimes Activity


These are answers submitted by Fabio92


Just some fun

restart:

Vectors

v1 := <4,4.1>:  # vector 1
v2 := <1,7.9>:  # vector 2
v3 := v2 - v1:  # vector 3
v4 := v2 + v1:  # vector 4

Utility functions

plot_arrow := proc(ori, vec, {ar_width := 0.03, ar_color := "Black", ar_head_width := 0.4, ar_head_length := 0.8, ar_linestyle := solid})
  plots:-arrow(
    ori,
    vec/~sqrt(vec.vec),
    length      = sqrt(vec.vec),
    width       = ar_width,
    shape       = arrow,
    color       = ar_color,
    head_width  = ar_head_width,
    head_length = ar_head_length,
    linestyle   = ar_linestyle);
end proc:

plot_text := proc(vec, {alignment := {right}, textcolor := "Black"})
  plots:-textplot(
    [vec[1], vec[2], cat("(",vec[1],", ",vec[2],")")],
    align = alignment,
    color = textcolor,   
    font  = [times, 12, bold]
  )
end proc:

Plots

plots:-display([
  plot_arrow([0,0], v1, ar_color = "Blue"),
  plot_arrow([0,0], v2, ar_color = "Purple"),
  plot_arrow([0,0], v3, ar_color = "Salmon", ar_linestyle = dash),
  plot_arrow(v1, v3, ar_color = "Red"),
  plot_text(v1, alignment = {right},       textcolor = "Blue"),
  plot_text(v2, alignment = {above,right}, textcolor = "Purple"),
  plot_text(v3, alignment = {left},        textcolor = "Red")
],
scaling   = constrained,
view      = [-10..10, -10..10],
labels    = [x, y],
title     = "Vector subtraction",
titlefont = [times, 20, bold]
);


plots:-display([
  plot_arrow([0,0], v1, ar_color = "Blue"),
  plot_arrow([0,0], v2, ar_color = "Purple"),
  plot_arrow(v2, v1, ar_color = "LightBlue", ar_linestyle = dash),
  plot_arrow(v1, v2, ar_color = "Violet", ar_linestyle = dash),
  plot_arrow([0,0], v4, ar_color = "Red"),
  plot_text(v1, alignment = {right},       textcolor = "Blue"),
  plot_text(v2, alignment = {above,left}, textcolor = "Purple"),
  plot_text(v4, alignment = {left},        textcolor = "Red")
],
scaling   = constrained,
#view      = [-5..15, -5..15],
labels    = [x, y],
title     = "Vector addition",
titlefont = [times, 20, bold]
)

 

 

 


 

Download vector_operation.mw

Another way is to make a procedure. (the procedure propose hereafter works only with matrices similar to the OP one!)


 

restart:
interface(rtablesize = 20):

foo1 := Matrix(4, 4, {(1, 1) = (12*L)*E/l^3, (1, 2) = (6*L)*E/l^2, (1, 3) = -(12*L)*E/l^3, (1, 4) = (6*L)*E/l^2, (2, 1) = (6*L)*E/l^2, (2, 2) = (4*L)*E/l, (2, 3) = -(6*L)*E/l^2, (2, 4) = (2*L)*E/l, (3, 1) = -(12*L)*E/l^3, (3, 2) = -(6*L)*E/l^2, (3, 3) = (12*L)*E/l^3, (3, 4) = -(6*L)*E/l^2, (4, 1) = (6*L)*E/l^2, (4, 2) = (2*L)*E/l, (4, 3) = -(6*L)*E/l^2, (4, 4) = (4*L)*E/l});

foo1 := Matrix(4, 4, {(1, 1) = 12*L*E/l^3, (1, 2) = 6*L*E/l^2, (1, 3) = -12*L*E/l^3, (1, 4) = 6*L*E/l^2, (2, 1) = 6*L*E/l^2, (2, 2) = 4*L*E/l, (2, 3) = -6*L*E/l^2, (2, 4) = 2*L*E/l, (3, 1) = -12*L*E/l^3, (3, 2) = -6*L*E/l^2, (3, 3) = 12*L*E/l^3, (3, 4) = -6*L*E/l^2, (4, 1) = 6*L*E/l^2, (4, 2) = 2*L*E/l, (4, 3) = -6*L*E/l^2, (4, 4) = 4*L*E/l})

(1)

collect_matrix := proc(mat)
  local i,j,k, tmp, idxeqs, idxtype, mask, multipl, idx_mul, out_mat, h, M,R;
  
  i,j     := LinearAlgebra[Dimension](mat);

  printf("Input matrix: %d x %d\n", i, j);
  
  mask    := [seq(X[k], k = 1..i*j)];
  tmp     := simplify(add(k,k=mask*~Vector[row](convert(mat, list))));
  idxeqs  := {ListTools[SearchAll](`+`, convert(whattype~(<op(tmp)>),list))};
  idxtype := {seq(k, k=1..numelems([op(tmp)]))};
  idx_mul := idxtype minus idxeqs:
  multipl := mul(k, k=[seq(op(idx_mul[k],tmp), k = 1..numelems(idx_mul))]);

  if nops(idxeqs) > 1 then
    error("Not implemented! You should do it.");
  else
    M,R := LinearAlgebra[GenerateMatrix]([op(idxeqs[1], tmp)], mask):
    M   := convert(M,list):
    if add(k,k=R) <> 0 then error("Something wrong!"); end if;
  end if:
  
  out_mat := Matrix(i,j);

  for h from 0 to j-1 do
    for k from 1 to i do
      out_mat[k,h+1] := M[h*i + k];
    end do:
  end do:

  return multipl , out_mat;

end proc:

collect_matrix(foo1);

Input matrix: 4 x 4

 

2*L*E/l^3, Matrix(4, 4, {(1, 1) = 6, (1, 2) = 3*l, (1, 3) = -6, (1, 4) = 3*l, (2, 1) = 3*l, (2, 2) = 2*l^2, (2, 3) = -3*l, (2, 4) = l^2, (3, 1) = -6, (3, 2) = -3*l, (3, 3) = 6, (3, 4) = -3*l, (4, 1) = 3*l, (4, 2) = l^2, (4, 3) = -3*l, (4, 4) = 2*l^2})

(2)

foo2 := <<9*a*b/c, a^2*3/c^2>|<6*a*b^2/c, 9*a/c>|<a^4*6*b/c^2, 3*a*b/c>>;
collect_matrix(foo2);

foo2 := Matrix(2, 3, {(1, 1) = 9*a*b/c, (1, 2) = 6*a*b^2/c, (1, 3) = 6*a^4*b/c^2, (2, 1) = 3*a^2/c^2, (2, 2) = 9*a/c, (2, 3) = 3*a*b/c})

 

Input matrix: 2 x 3

 

3*a/c^2, Matrix(%id = 18446744074372522702)

(3)

 


 

Download nice_multiply_collect_matrix.mw

From algsubs page in the Maple Help:

"Note that the requirement for monomials in a to divide monomials in f means that the negative powers of u in the following example are not substituted, and must be handled separately as shown."

https://www.maplesoft.com/support/help/maple/view.aspx?path=algsubs

The following code should do what you are looking for

expr := a/w^2;
algsubs(w^(-2) = V1, expr);

or 

expr := a/w^2;
algsubs(1/(w^2) = V1, expr);

 

You are trying to access an element of "a" with a non integer subscript; you may try to ceil or floor "c"

example: assume that x := [3,2,1];

medianmean := proc(x) local a,b,c,d,m,g;
  a := sort(x);           # sorting x -> [3,2,1] becomes [1,2,3]
  b := nops(x);           # nops(x) is 3
  c := nops(x)/2;         # nops(x)/2 is 1.5
  d := (nops(x)+1)/2;     # gives 4/2 = 2
  m := (a[c]+a[c+1])/2;   # here will raise an error while trying to access a[c] = a[1.5]
  g := add(a)/b;

  if (b mod 2 = 0) then 
    evalf([a,m,g]) ;
  else 
    evalf([a,a[d],g]);
  end if; 
end proc:

Looks like you already assigned varible x2 somewhere in your maple sheet (something like x2 := 0.8). 

Since you already assigned x1 value to 5 I don't understand why you are using plot3d (f1 becomes a 1-d function); maybe you wanted to plot the surface f(x1, x2)? If so try something like:

f1:= unapply(3.579167+3.537500*x1-2.645833*x2-0.250000*x1*x1-0.012500*x2*x1+0.175000*x2*x2, x1, x2);
plot3d (f1(var1,var2), var1 = -1..1, var2 = 3..7, labels=[ginseng, grape, preference], axes=boxed);

 

To denote mathematical infinity you have to use infinity (all lowercase) and not Infinity

Using plot/discont option Maple will detect discontinuities and divides the plotting range into subranges over which the plot is continuous.

pw := piecewise(x < -2, 2, -2 <= x and x <= 5, 2*x+5, 5 < x, 1-sqrt(x-5)); 
plot(pw, discont = true)

?plot/options

Edit: added colon as pointed out by Carl

Assuming you are using plottools and plot commands,try to change order in which you display your manipulator "bodies".

display([
  plottools[line](point1, point2)
  plottools[line](point3, point4)
]);

gives different result compared to

display([
  plottools[line](point3, point4)
  plottools[line](point1, point2)
]);

if the two graphical objects overlap.

Here an example -> plot_order.mw

You can start Maple using a shared kernel; by default Maple uses parallel kernels one for each worksheet; to do that you need to append -km s to operating system line that load Maple;

Example on Windows machines -> create a shortcut -> properties and modify the link "Target" line

"C:\the_directory_in_which_Maple_is_installed\maplew.exe"

into

"C:\the_directory_in_which_Maple_is_installed\maplew.exe" -km s

https://www.maplesoft.com/support/help/Maple/view.aspx?path=worksheet/reference/kernelmodes

You can use display command to plot a set or list of plot istances on the same plot; you can add the following code to yours

with(plots):  # load "plots" package

display(
  [
    plot(trial,      x=0..1, legend = ['trial'], color = "Red"),
    plot(rhs(exact), x=0..1, legend = ['exact'], color = "Blue")
  ],
 
  title     = "Trial vs Exact",
  labels    = [label__X, label__Y],
  gridlines = true
);

 

Page 1 of 1