Imagine I define two variables within a MuPad Notebook:
x:=2;
y:=5
For the product
z=x*y
I get displayed:
And if I use hold, I can get the expression:
z=hold(x*y)
But now I'd like to have both, the expression displayed and the result. The two options which appeared logical to me, do not worK:
z=hold(x*y);z
and
z=hold(x*y);eval(z);
How can I get displayed the expression AND the result?
If in two lines it would be alright, but I'd prefer in one line like:
z = x y = 10
I tried some combinations with print, expr2text, hold and _concat but couldn't find a convincing solution to get the desired result. But there is an explanation why the second line just returns z and not 10.
Assignment vs. Equation
z is the result in the second line because you didn't assign something to z yet. So the result says that z is z. In MuPad = is part of an expression. The assignment operator is := and therefore not the same as in Matlab. The only difference between them is the colon.
Writing an equation
For writing an equation, we use = as part of the expression. There is an equivalent function: _equal. So the following two lines generate the same result:
x+y = 2
_equal(x+y, 2)
Assign value to x
For an assignment we use := (in Matlab this would be only =). There is an equivalent function: _assign. So again, the following two lines generate the same result:
x := value
_assign(x, value)
Assign the equation x+y = 2 to eqn
Here we can clearly see the difference:
eqn := x+y = 2
_assign(eqn, _equal(x+y, 2))
Related
I have this simple procedure in Maple that I want to plot.
test:=proc(n)
local i,t;
for i from 1 to n do
t:=1;
od;
return t;
end:
The procedure itself works fine.
> test(19)
1
When I want to plot I get the following Error:
> plot(test(x),x=1..10)
Error, (in test) final value in for loop must be numeric or character
Please help
Maple's usual evaluation model is that arguments passed to commands are evaluated up front, prior to the computation done within the body of the command's own procedure.
So if you pass test(x) to the plot command then Maple will evaluate that argument test(x) up front, with x being simply a symbolic name.
It's only later in the construction of the plot that the plot command would substitute actual numeric values for that x name.
So, the argument test(x) is evaluated up front. But let's see what happens when we try such an up front evaluation of test(x).
test:=proc(n)
local i,t;
for i from 1 to n do
t:=1;
od;
return t;
end:
test(x);
Error, (in test) final value in for loop
must be numeric or character
We can see that your test procedure is not set up to receive a non-numeric, symbolic name such as x for its own argument.
In other words, the problem lies in what you are passing to the plot command.
This kind of problem is sometimes called "premature evaluation". It's a common Maple usage mistake. There are a few ways to avoid the problem.
One way is to utilize the so-called "operator form" calling sequence of the plot command.
plot(test, 1..10);
Another way is to delay the evaluation of test(x). The following use of so-called unevalution quotes (aka single right ticks, ie. apostrophes) delays the evaluation of test(x). That prevents test(x) from being evaluated until the internal plotting routines substitute the symbolic name x with actual numeric values.
plot('test(x)', x=1..10);
Another technique is to rewrite test so that any call to it will return unevaluated unless its argument is numeric.
test:=proc(n)
local i,t;
if not type(n,numeric) then
return 'procname'(args);
end if;
for i from 1 to n do
t:=1;
od;
return t;
end:
# no longer produces an error
test(x);
test(x)
# the passed argument is numeric
test(19);
1
plot(test(x), x=1..10);
I won't bother showing the actual plots here, as your example produces just the plot of the constant 1 (one).
#acer already talked about the technical problem, but your case may actually have a mathematical problem. Your function has Natural numbers as its domain, i.e. the set of positive integers {1, 2, 3, 4, 5, ...} Not the set of real numbers! How do you interpret doing a for-loop for until a real number for example Pi or sqrt(2) to 5/2? Why am I talking about real numbers? Because in your plot line you used plot( text(x), x = 1..10 ). The x=1..10 in plot is standing for x over the real interval (1, 10), not the integer set {1, 2, ..., 10}! There are two ways to make it meaningful.
Did you mean a function with integer domain? Then your plot should be a set of points. In that case you want a points-plot, you can use plots:-pointplot or adding the option style=point in plot. See their help pages for more details. Here is the simplest edit to your plot-line (keeping the part defininf test the same as in your post).
plot( [ seq( [n, test(n)], n = 1..10 ) ], style = point );
And the plot result is the following.
In your function you want the for loop to be done until an integer in relation with a real number, such as its floor? This is what the for-loop in Maple be default does. See the following example.
t := 0:
for i from 1 by 1 to 5/2 do
t := t + 1:
end do;
As you can see Maple do two steps, one for i=1 and one for i=2, it is treated literally as for i from 1 by 1 to floor(5/2) do, and this default behavior is not for every real number, if you replace 5/2 by sqrt(2) or Pi, Maple instead raise an error message for you. But anyway the fix that #acer provided for your code, plots the function "x -> test(floor(x))" in your case when x comes from rational numbers (and float numbers ^_^). If you change your code instead of returning constant number, you will see it in your plot as well. For example let's try the following.
test := proc(n)
local i,t:
if not type(n, numeric) then
return 'procname'(args):
end if:
t := 0:
for i from 1 to n do
t := t + 1:
end do:
return(t):
end:
plot(test(x), x=1..10);
Here is the plot.
It is indeed "x -> test(floor(x))".
I think I am missing something very basic here
function F = non_iter(x,kc,kw)
F = [x(6)*x(4)*kc-3*x(2);
x(2)*x(5)-kw*x(6)*x(3);
x(2)+x(6)-1;
x(1) -7.52;
x(6)+2*x(4)+2*x(2)+x(3)-4;
x(3)+x(5)-8];
end
when I call this, like
fu = non_iter(x,kc,kw)
It says vertical dimension mismatch (3x1 vs 1x2)
x is supposed to be a row vector of length 6 and kc and kw are scalars.
I have other functions with vector and scalar arguments but they don't return a column vector.
I tried with this function in separate file as well as make it inline with # operator.
This version is working for me on Matlab.
function F = non_iter(x,kc,kw)
F = [x(6)*x(4)*kc-3*x(2);
x(2)*x(5)-kw*x(6)*x(3);
x(2)+x(6)-1;
x(1)-7.52;
x(6)+2*x(4)+2*x(2)+x(3)-4;
x(3)+x(5)-8];
end
In the 4th row of F the white space is treated as a separator for two entries, it should be removed. Additionally, there is an unnecessary bracket.
I've got some code to numerically solve for eigenvectors:
function[efun,V,D] = solveeig(n,xmax,i)
for j=1:i
%The first and second derivative matrices
dd = 1/(xmax/n)^2*(-2*diag(ones(n,1))+diag(ones(n-1,1),1)+...
diag(ones(n-1,1),-1));
d = 1/(xmax/n)*((-1*diag(ones(n,1)))+diag(ones(n-1,1),1));
%solve for the eigenvectors
[V,D] = eig(-dd-2*d);
%plot the eigenvectors (normalized) with the normalized calculated
%eigenfunctions
x = linspace(0,xmax,n);
subplot(i,1,j);
plot(x,V(:,j)/sum(V(:,j)),'*');
hold on
efun = exp(-x).*sin(j*pi*x/xmax);
plot(x,efun/(sum(efun)),'r');
shg
end
end
i is supposed to be the first i eigenvectors, n is the dimension of the
matrices (the number of pieces we discretize x into), xmax is the upper limit of the range on which the fxn is defined.
I'm trying to run this from the command line (as: "solveeig # # #", where the number signs correspond to i, n, and xmax) but no matter what I seem to put in for i, n, and xmax, I get "For colon operator with char operands, first and last operands must be char."
What should I be writing on the command line to get this to run?
Using the command syntax interprets the arguments as strings
For fuller details see the documentation but in short:
Calling
myFun myVar1 6 myVar2
is equivalent to calling
myFun('myVar1','6','myVar2')
and not the desired1
myFun(myVar1,6,myVar2)
In the first cases the function will receive 3 strings (text)
In the second the function will receive the data stored in myVar1 myVar2 and the number 6
The specific error you received is caused by line 2 for j=1:i here i is a string. This error is a merely consequence of the way the function has been called, the line itself is fine2.
How to get it to work
Use function syntax: in the command window something like:
solveeig(n,xmax,i)
If command syntax is absolutely required ( and I can't think why it would be) the much less favourable alternative would be to parse the strings inputted in command syntax. To convert the numbers into numeric formats and use evalin/assignin on the passed variable names to pull variables in from the caller
1As mentioned in comments by patrik
2meaning it won't error, however i and j as variable names is another matter
The code is
syms x;
v = #(x) (4 - x^2)^(1/2) - (2 - x)^(1/2)*(x + 2)^(1/2);
ezplot(v(x),[-2,2]);
which produce a plot
Should not v(x)
be zero for every x in [-2,2]?
If the idea is to replace "small" values in the output of your equation with zero, then some sort of if condition on the output data is needed. Unfortunately, you can't add an if to the anonymous function but you can do one of two things: define a standard function (in a program file) or wrap your anonymous function in another. The first way is more straightforward:
function [y] = v_func(x)
y = (4 - x.^2).^(1/2) - (2 - x).^(1/2).*(x + 2).^(1/2);
y(abs(y)<1e-10)=0;
end
The above performs the same calculation as before with second line of code replacing all values that are less than some tolerance (1e-10) with zero. Note how the equation is slightly different than your original one. It has been "vectorized" (see the use of the periods) to allow for an input vector to be evaluated rather than having to loop over each element in the vector. Note also that when we pass it to ezplot we must prefix the function name with the ampersand:
ezplot(#v_func,[-2,2]);
The second way requires wrapping your first anonymous function v in a couple of others (see anonymous functions and conditions). We start with your original function that has been vectorized:
v = #(x) (4 - x.^2).^(1/2) - (2 - x).^(1/2).*(x + 2).^(1/2);
and a (condition) function that replaces all values less than some tolerance with zero:
cond = #(y)(y*(1-(abs(y)<1e-10)))
We then wrap the two together as
func = #(x)cond(v(x))
so that the condition function evaluates the output of v. Putting it together with ezplot gives:
ezplot(func,[-2,2]);
and all output, as shown in the plot, should be zero.
The figure title will not be what you want, so it can be replaced with some variation of:
plotTitle = func2str(v);
title(strrep(plotTitle(5:end),'.','')); % as I don't want '#(x)' or periods to
% appear in the title
I've ignored the use of sym as I don't have the Symbolic Toolbox.
I am trying to use Octave's fminsearch function, which I have used in MATLAB before. The function seems not sufficiently documented (for me at least), and I have no idea how to set to options such that it would actually minimize.
I tried fitting a very simple exponential function using the code at the end of this message. I want the following:
I want the function to take as input the x- and y-values, just like MATLAB would do. Furthermore, I want some control over the options, to make sure that it actually minimizes (i.e. to a minimum!).
Of course, in the end I want to fit functions that are more complicated than exponential, but I want to be able to fit exponentials at least.
I have several problems with fminsearch:
I tried handing over the x- and y-value to the function, but a matlab-style thing like this:
[xx,fval]=fminsearch(#exponential,[1000 1],x,y);
or
[xx,fval]=fminsearch(#exponential,[33000 1],options,x,y)
produces errors:
error: options(6) does not correspond to known algorithm
error: called from:
error: /opt/local/share/octave/packages/optim-1.0.6/fmins.m at line 72, column 16
error: /opt/local/share/octave/packages/optim-1.0.6/fminsearch.m at line 29, column 4
Or, respectively (for the second case above):
error: `x' undefined near line 4 column 3
error: called from:
error: /Users/paul/exponential.m at line 4, column 2
error: /opt/local/share/octave/packages/optim-1.0.6/nmsmax.m at line 63, column 6
error: /opt/local/share/octave/packages/optim-1.0.6/fmins.m at line 77, column 9
error: /opt/local/share/octave/packages/optim-1.0.6/fminsearch.m at line 29, column 4
Apparently, the order of arguments that fminsearch takes is different from the one in MATLAB. So, how is this order??
How can I make fminsearch take values and options?
I found a workaround to the problem that the function would not take values: I defined the x- and y values as global. Not elegant, but at least then the values are available in the function.
Nonetheless, fminsearch does not minimize properly.
This is shown below:
Here is the function:
function f=exponential(coeff)
global x
global y
X=x;
Y=y;
a= coeff(1);
b= coeff(2);
Y_fun = a .* exp(-X.*b);
DIFF = Y_fun - Y;
SQ_DIFF = DIFF.^2;
f=sum(SQ_DIFF);
end
Here is the code:
global x
global y
x=[0:1:200];
y=4930*exp(-0.0454*x);
options(10)=10000000;
[cc,fval]=fminsearch(#exponential,[5000 0.01])
This is the output:
cc =
4930.0 5184.6
fval = 2.5571e+08
Why does fminsearch not find the solution?
There is an fminsearch implementation in the octave-forge package "optim".
You can see in its implementation file that the third parameter is always an options vector, the fourth is always a grad vector, so your ,x,y invocations will not work.
You can also see in the implementation that it calls an fmins implementation.
The documentation of that fmins implementation states:
if options(6)==0 && options(5)==0 - regular simplex
if options(6)==0 && options(5)==1 - right-angled simplex
Comment: the default is set to "right-angled simplex".
this works better for me on a broad range of problems,
although the default in nmsmax is "regular simplex"
A recent problem of mine would solve fine with matlab's fminsearch, but not with this octave-forge implementation. I had to specify an options vector [0 1e-3 0 0 0 0] to have it use a regular simplex instead of a 'right-angled simplex'. The octave default makes no sense if your coefficients differ vastly in scale.
The optimization function fminsearch will always try to find a minimum, no matter what the options are. So if you are finding it's not finding a minimum, it's because it failed to do so.
From the code you provide, I cannot determine what goes wrong. The solution with the globals should work, and indeed does work over here, so something else on your side must be going awry. (NOTE: I do use MATLAB, not Octave, so those two functions could be slightly different...)
Anyway, why not do it like this?
function f = exponential(coeff)
x = 0:1:200;
y = 4930*exp(-0.0454*x);
a = coeff(1);
b = coeff(2);
Y_fun = a .* exp(-x.*b);
f = sum((Y_fun-y).^2);
end
Or, if you must pass x and y as external parameters,
x = [0:1:200];
y = 4930*exp(-0.0454*x);
[cc,fval] = fminsearch(#(c)exponential(c,x,y),[5000 0.01])
function f = exponential(coeff,x,y)
a = coeff(1);
b = coeff(2);
Y_fun = a .* exp(-x.*b);
f = sum((Y_fun-y).^2);
end