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 ?
Related
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?
One of my students made the following simple error:
f = '#(x)sqrt(1-x^2)';
quad(f,0,1)
which led to a very unexpected (to me) result:
ans =
0 + 1.7119i
Presumably # is getting interpreted as some complex function, but which?
I want to use an integral over a vector but I will see an error for sys_eff which is " First input argument is not function handle."
I will be glad to have your guide and thanks in advance.
I should mention that all vectors have the same ize as 345600.
function [ P_loss,time_eff, sys_eff ] = final( Pmpp, Pl_inv, Pl_bat_inv, Pl_bat_r )
j=length(Pmpp);
for t=1:j;
P_loss(t)= Pl_inv(t) + Pl_bat_inv(t) + Pl_bat_r(t);
time_eff(t)= P_loss(t)/Pmpp(t);
end
sys_eff=integral(time_eff,0,345600);
end
As from the error message, the first input you provided to the function integral (i.e. time_eff) is not a function handle, but a vector.
If you want to make a numeric integral of the function use the function trapz
sys_eff=trapz(t,time_eff)
if t is your integration variable.
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)
It looks like this has been asked many times, but none of the past posts seem to solve my question. All those had to do with matrix/vector while my code does not have any of these, just simple variables. It takes three variables as arguments. It works perfectly fine within the Matlab environment. I only got the error when I compiled it with mcc -m Normal.m and tried to run with the executable like this "./Normal 1 5 0.5". The complete error message is:
Error using /
Matrix dimensions must agree.
Error in Normal (line 4)
MATLAB:dimagree
It is complaining about line 4: N=2/dt, what is wrong with this?
Here is the code:
function val=Normal(l1,l2,dt)
const=(l2/l1-1);
N=2/dt;
S=1.0/sqrt(l2/l1);
Z(1)=S;
for i=2:N
t= -1+(i-1)*dt;
Z(i)=1.0/sqrt(const*t*t+1);
S=S+2*Z(i);
end
Z(21)=1.0/(l2/l1);
S=S+1.0/sqrt(l2/l1);
val=dt*S/2;
end
But dt is not a scalar when passed into the standalone through the command ./Normal 1 5 0.5. It is a character array with 3 elements ('0', '.','5')!
When passing numerical arguments to a standalone, they are passed as strings. Thus, inside the function, you need to convert '0.5' into a double, and similarly for l1 and l2:
dt = str2num(dt);
l1 = str2num(l1);
l2 = str2num(l2);
Note that you can use isdeployed to determine at runtime if the function is a standalone:
if isdeployed, dt = str2num(dt); end
And you might need to display the result:
if isdeployed, disp(val); end
Result:
>> system('Normal 1 5 0.5');
1.4307
>> Normal(1,5,0.5) % .m function for comparison
ans =
1.4307