sympref --- 'MatrixWithSquareBrackets' not work - matlab

I used sympref('MatrixWithSquareBrackets',true).But raise an error:
Expected input to match one of these values:
'FourierParameters', 'HeavisideAtOrigin', 'AbbreviateOutput', 'TypesetOutput'
The input, 'MatrixWithSquareBrackets', did not match any of the valid values.
Error in Untitled3 (line 7)
sympref('MatrixWithSquareBrackets',true);
I want to use symbolic matrix square brackets in my answer.How can I do this?

Related

Undefined operator '.*' for input arguments of type 'cell'

for i=1:3
S=sum(w(i).*srmap{i});
end
Code above causes following error:
Undefined operator '.*' for input arguments of type 'cell'.
Error in test1 (line 23)
S=sum(w(i).*srmap{i});
What is the meaning of this error?
w(i) is a number and srmap{i} is a matrix.

How do take natural log of a double in MATLAB?

I am attempting to that the natural log of a number, I get the message:
tf2 = 60*ln(B1);
Undefined function 'ln' for input arguments of type 'double'.
So i try to cast the number as a float which the documentation claims it will accept but
then i get the error message :
float(B1);
Error using float (line 50)
The input argument to float was not a supported type. The only recognized strings are 'single' and 'double'. The input type was 'double'
So then i try to cast the double as a single and get the same error but it says :
f=single(B1);
float(B1);
Error using float (line 50)
The input argument to float was not a supported type. The only recognized strings are 'single' and 'double'. The input type was 'single'
The natural log in MATLAB is simply log(x). You're mixing the two:
log in MATLAB
log in MuPAD
The error message you get is because the function is not defined. You'll get the same error for this line:
bogus_function(1.23)
??? Undefined function or method 'bogus_function' for input arguments
of type 'double'.
I know it's an old question but as I didn't find a good answer when I was trying to do it so I will write my solution for others.
First there is no implemented function to do ln operation in matlab, but we can make it.
just remember that the change formula for log base is
log b (X)= log a (X)/log a (B)
you can check this easily.
if you want to calculate log 2 (8)
then what you need to do is to calculate log 10 (8)/log 10 (2)
you can find that: log 2 (8) = log 10 (8)/log 10 (2) = 3
So easily if you want to calculate ln(x), all you need is to change the base to the e.
ln(x) = log 10 (x)/log 10 (e)
so, just write that code in matlab
my_ln= log 10 ( number ) / log 10 ( exp(1) );
you can also make it as a function and call it whenever you need it,
function [val] = ln_fun(number)
val = log 10 (number)/ log 10 ( exp(1) );
end
*remember the log general formula → log base (number)

fzero: Too many output arguments

To learn how to work with fzero() I tried this code:
function equation(x)
k=(96-x)/6;
end
and then:
x0=4;
x=fzero('equation',x0)
The error is:
??? Error using ==> fzero at 307
FZERO cannot continue because user supplied function_handle ==> equation
failed with the error below.
Too many output arguments.
fzerois expecting a return value from your equation so (internally) it is trying to assign something to the output of that function. If I try
result = equation(42);
I get the same error message
Error using equation
Too many output arguments.
Just change your function signature to
function [k] = equation(x)
to indicate that k is the output of this function.
Try to supply the function argument as handle:
x = fzero(#equation,x0)

How do I use MATLAB's inputParser with optional string inputs? The documentation says "use a validation function" but it's unclear how to do that

I have a MATLAB file that contains a single top-level function, called sandbox. That function in turn contains two nested functions, mysum and myprod, which are identical in functionality and what parameters they allow except that one uses #sum internally and the other uses #prod internally. My goal is to create a wrapper function to use in both mysum and myprod that takes care of all the validation and input parsing. This function is called applyFunc.
Here's where it gets tricky. mysum and myprod come in two forms:
mysum(v) returns sum(v, 1).
mysum(v, 'imag') returns sum(v, 1) + 1i
Any other combinations of input should throw an error.
I'm having trouble using inputParser to parse these various combinations of input, specifically the optional string input. Here's the code:
function sandbox()
%% Data
v = [1 4; 3 3];
%% Calculations
s = mysum(v);
si = mysum(v, 'imag');
p = myprod(v);
pi = myprod(v, 'imag');
%% Accuracy tests
assert(isequal(s, [4 7]))
assert(isequal(si, [4+1i 7+1i]))
assert(isequal(p, [3 12]))
assert(isequal(pi, [3+1i 12+1i]))
function x = mysum(varargin)
x = applyFunc(#sum, varargin{:});
end
function x = myprod(varargin)
x = applyFunc(#prod, varargin{:});
end
end
function x = applyFunc(func, varargin)
p = inputParser();
p.addRequired('func', #(x) validateattributes(x, {'function_handle'}, {'scalar'}));
p.addRequired('v', #(x) validateattributes(x, {'double'}, {}, 'applyFunc:msg', 'v'));
p.addOptional('imag', '', #(x) validatestring(x, {'imag', ''})); % THIS LINE IS THE PROBLEM
p.parse(func, varargin{:});
f = p.Results.func;
v = p.Results.v;
strflag = p.Results.imag;
x = f(v);
if ~isempty(strflag)
validatestring(strflag, {'imag'});
x = x + 1i;
end
end
The line that's causing the problem is this one (as marked in the code above):
p.addOptional('imag', '', #(x) validatestring(x, {'imag', ''}));
The documentation for inputParser says that:
For optional string inputs, specify a validation function. Without a validation function, the input parser interprets valid string inputs as invalid parameter names and throws an error.
Unfortunately I don't have any idea how to do this. Is there something simple Im missing or what? If the 'imag' argument isn't passed at all (as in the assignment of s and p), the code works fine, but if I do pass it, I get this error:
Error using sandbox>applyFunc (line 32)
The value of 'imag' is invalid. It must satisfy the function:
#(x)validatestring(x,{'imag',''}).
Error in sandbox/mysum (line 18)
x = applyFunc(#sum, varargin{:});
Error in sandbox (line 7)
si = mysum(v, 'imag');
Any help?
The problem is that validatestring returns the matching string from the cell argument ({'imag',''}) rather than a Boolean indicating if it passes validation. Instead, use strcmp and any:
#(x) any(strcmp(x,{'imag', ''}))
Also, with validatestring, if the input string did not match either 'imag' or '' (actually just 'imag' since empty strings only match in R2014a+), it would throw an error rather than returning false so that the inputParser could return the appropriate error.
Another nice way to fix the problem is to change the syntax of applyFunc entirely so that instead of just 'imag' as an optional string input argument, use a Parameter-Value with 'imag' as the parameter and a validated boolean as the input.
The input definition suggested by Amro in the comments:
p.addParameter('imag', false, #(x)validateattributes(x, {'logical'}, {'scalar'}))
The usage:
mysum(x,'imag',true)
mysum(x) % default is equivalent to mysum(x,'imag',false)
This would simplify the rest of the code with p.Result.imag being a logical scalar. I would suggest:
x = f(v) + p.Result.imag*1i;
The problem is not inputParser, I think the issue is with validatestring.
1) First it does not match on empty strings:
>> x = ''
x =
''
>> validatestring(x, {'imag',''})
Expected input to match one of these strings:
imag,
The input did not match any of the valid strings.
Caused by:
Error using validatestring>checkString (line 85)
Expected input to be a row vector.
2) Second, if it successfully matches, it returns the resolved string (from one of the valid choice), instead of true/false. inputParser requires that the validation function either return a boolean, or nothing but throws error on failure.

Using elementwise operation and element indexing together in Matlab

I want to write an anonymous function taking a vector theta as its input and compute the sum of the fourth-squared of the first half elements of theta:
L=#(theta) sum(theta.^4(1:length(theta)/2))
But Matlab reports an error
??? Error: File: c2n9.m Line: 3 Column: 27
Unbalanced or unexpected parenthesis or bracket.
I identified the error same as the following simpler example
>> a=ones(1,4)
a =
1 1 1 1
>> a.^4(1:2)
??? a.^4(1:2)
|
Error: Unbalanced or unexpected parenthesis or bracket.
>> (a.^4)(1:2)
??? (a.^4)(1:2)
|
Error: Unbalanced or unexpected parenthesis or bracket.
I wonder how I can make the simple example as well as the anonymous function work?
Thanks!
You could instead do
a(1:2).^4
you should do the indexing before the per-element raising to the power
instead of:
L=#(theta) sum(theta.^4(1:length(theta)/2))
try
L=#(theta) sum(theta(1:round(length(theta)/2)).^4)
note that I also added a round to take care of the case where the length of theta is odd
Err, aren't you missing a multiplication sign at the place that the first error message points to ? Or something else ?