How to create collect function generality - maple

I have a problem in maple, someone can help me.
I want to write collect function generality in maple. Example, i have
f := a(ak+bk+c)+b(bk+ck+a)+c(ak+ck+b)
I call gcollect(f,a^2+b^2+c^2) then we get
k(a^2+b^2+c^2)+abk+ack+bck+ab+ac+bc,
or gcollect(f,a^2+b^2) then we get
k(a^2+b^2)+kc^2+abk+ack+bck+ab+ac+bc.
Thank you very much.

Here's something to start with..
restart:
gcollect:=(expr,t)->
thaw(collect(algsubs(t=freeze(t),
expand(expr)),freeze(t),_rest)):
f := a*(a*k+b*k+c)+b*(b*k+c*k+a)+c*(a*k+c*k+b):
gcollect(f, a^2+b^2+c^2);
gcollect(f, a^2+b^2+c^2, expand);
gcollect(f, a^2+b^2);
gcollect(f, a^2+b^2, expand);

Related

Maple strange results while solving for complex equation

I'm trying to make Maple solve a complex equation, but it produces an incorrect result.
The following images tells it all :
At (3) I would expect to get something close to 1 (as (2) shows), yet it gives me something that doesn't make any sense. Is it that the || (to express the complex number modulus) operator has another significance in the solve() function?
The more appropriate function here is fsolve.
Example 1
restart:
G:=(w,L)->(5+I*L*2*Pi*w)/(150+I*L*2*Pi*w);
evalf(5*abs(G(10,1)));
fsolve(5*abs(G(10,L))=%,L=0..10)
Example 2
As above, you need to specify the interval L=0..1 where the solution might be.
G:=(f,L)->(256.4+I*L*2*Pi*f)/(256.4+9845+I*L*2*Pi*f);
evalf(5*abs(G(20000,0.03602197444)));
fsolve(5*abs(G(20000,L))=%,L=0..1);
If you are facing difficulties to specify the interval then you should plot it first, it will give you an idea about it?
plot(5*abs(G(20000,L)),L=0..1)
Restrict the values of L in the solve command with the assuming command.
Example 1:
G:= (w,L) -> (50+I*L*2*Pi*w)/(150+I*L*2*Pi*w);
result := evalf(5*abs(G(10,1)));
solve({5*abs(G(10,L)) = result},L) assuming L::real;
{L = 1.000000000}, {L = -1.000000000}
Example 2:
G:=(f,L) -> (256.4+I*2*Pi*L*f)/(256.4+9845+I*2*Pi*L*f);
result := 5*abs(G(20000,0.03602197444));
solve({5*abs(G(20000,L)) = result},L) assuming L::real;
{L = 0.03602197445}, {L = -0.03602197445}

Invalid Input: Second argument is missing

I'm trying to use Newton's method to approximate where the gradient of a function = 0 But I'm guessing error messages when I try to put in guesses for an iteration of Newton's, and I don't know what the problem is.
(For reference, Profit = 144TVa - 0.07TVb TVb - 0.01 TVa^2 + 174TVb -0.01TVb^2 - 4E5)
with(LinearAlgebra):
with(VectorCalculus):
DProfit := Gradient(Profit, [TVa, TVb])
F := unapply(DProfit, TVa, TVb)
J := Jacobian(DProfit)
Guess := V->evalf(V-Multiply(Jinv,F(V))):
But when I try to evaluate Guess at any points, it gives me an error:
Guess(3000,3000)
Error, (in Guess) invalid input: F uses a 2nd argument, TVb, which is missing
Guess(<3000,3000>
Error, (in Guess) invalid input: F uses a 2nd argument, TVb, which is missing
even though F(3000,3000) returns [-66,-36]
Thanks for the help.
Figured out how to fix it!
Instead of:
Guess := V->evalf(V-Multiply(Jinv,F(V))):
I did:
Guess := V->evalf(V-Multiply(Jinv,F(V[1],V[2]))):

How to access a matrix to a m file from another m file?

Suppose the matrix is A which is in a m file new1.m . Now I want to access this matrix to another m file new2.m . How can it be done ?
Your question is a little nonspecific, but I'll try to answer.
There are several ways to do this,
Assuming that you have a m-file (script) named 'new1' with (for example) A = rand(4) in it. You could just run it in new2.m before you want to use A
new1;
B = 2*A;
Note that new1 will return all the other variables assigned in it, flooding your workspace. Perhaps not a problem, but if so, you could just clear them with
clear var1 var2 var2 etc.
Another way is to make new1 into a function and return (only) A
function A = new1()
but I'm guessing that might ruin some other purposes of new1.
In that case you could return A only if the function is called with a special input argument (for example 'getA')
function new1(varargin)
...
... % some code
...
if nargin && strcmp(varargin{1},'getA')
assignin('caller','A',A);
end
And so from new2, just call the function.
new1('getA');

Expression to be reduced by using Boolean algebra

Hi I have this expression XY'Z+YY'+X+XY'+XZ ( ' means not) Can anyone please show me the simplification of this expression? Thanks in advance.
This is what i got so far:
XY'Z+YY'+XY'+X(1+Z)
XY'Z+YY'+XY'+XY'+X
Y'(XZ+Y)+X(1+Y')
Y'(XZ+Y)+X
I know 1+Y=1 but 1+Y'=1 too?
Y'(XZ+Y)+X
change this back to:
XY'Z+YY'+X
Then simplify a bit:
XY'Z+YY'+X
YY' + X(1+Y'Z)
0 + X
X

Elusive Matlab variable syntax error

u and v and r should be vectors.
function [g] = kast(m,k,u,v,n)
g = 9.80;
t = 0:0.1:n;
r = [u.*(m/k).*(1-exp(-k.*t./m),((-m*g/k).*t) + (v.*(m/k).+m^2*g/k^2).*( 1.-exp(-k.*t./m)))];
plot(t,r)
end
I've spent about an hour, but I cannot work out what is wrong. Are any of you able to spot my error?
Thank you for your time.
Kind regards,
Marius
.+m^2
looks suspicious. Try remove the dot before the +. And add a closing bracket before the comma in the assignment of r.