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]))):
Related
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}
I've got such a problem using MATLAB:
I wrote this function:
function E = f(x, lamda)
E = 1 - exp(-lamda * x);
end
When I write: Prob = f(1000, lamda); where lamda = 3.4274e-004
I get this error:
??? Attempted to access f(1000,0.000341565); index must be a positive integer or logical.
I understand that it requires a positive integer, but why ? I need lamda to be real. What's the problem here ? Can you, please, tell me where I'm wrong?
You have a function f and a variable f declared at the same time. Do clear f; then try your code again. What's happening here is that the variable declaration takes precedence over your function and so doing f would try and access the variable f first.
If you're using f as a variable somewhere and can't change this, then rename your function to be something other than f... perhaps... comp or something. Once you do this, make sure you change your file name so that it's called comp.m, then do:
Prob = comp(1000, lamda);
Your error message indicates that there is a variable called f in your workspace and matlab thinks you are trying to access its elements. Remove the variable f with clear('f') or rename the function to something else and you should be fine.
I am using openmodelica and I am trying to loop through an array in order to find the maximum value. I was able to reduce my code to a very simple test case that still gives the error. Is this something that I am doing wrong, or is this a bug in openmodelica? Here is a very simple case that does give the error:
package TestLoop
model ItemA
Real p;
end ItemA;
model ItemB
ItemA a[n];
parameter Integer n = 5;
Real p;
equation
for i in 1:n loop
a[i].p = time;
end for;
algorithm
for i in 1:n loop
p := a[i].p;
end for;
end ItemB;
end TestLoop;
The problem is in my algorithm section. Here is the error that I am getting:
TestLoop.ItemB.c:155:13: warning: implicit declaration of function '$Pa$lB' is invalid in C99 [-Wimplicit-function-declaration]
$Pp = $Pa$lB(modelica_integer)$Pi$rB$Pp;
^
TestLoop.ItemB.c:155:20: error: unexpected type name 'modelica_integer': expected expression
$Pp = $Pa$lB(modelica_integer)$Pi$rB$Pp;
^
1 warning and 1 error generated.
Any suggestions for why this might be, or how I can work around it? If I replace the assignment with a fixed value, p:=a[1].p;, the code does run (although that is not useful to me). What I ultimately want to do in the algorithm section is find the largest value of a[n].p, where I do have an equation section that does useful calculations into the array of items.
Yes, the code generation is an error of OpenModelica (it does not like unknown array indexes). Your problem is very easy to solve in a single line though (one of the following):
p = max(r for r in a.p);
p = max(a.p);
I'm working with someone else's code and I am unfamiliar with try/catch so I made a small, similar example. On line 11, if I write error(''), it doesn't seem to catch the error and increase the index j. However, writing error(' ') or error('bad!') does.
So does having an error with an empty string ignore the error, or am I doing something wrong?
% Just a file to understand the Matlab command try/catch
M = 3;
j = 1;
k = [Inf, 5, 4];
while M>0
try
M = M-1
u = k(j)
if (isinf(u)||isnan(u)), error(''), end;
catch
j = j+1
end
end
Yes, error('') and error([]) and error(struct([])) all do not actually display an error message and abort running code. I personally consider the use of the single string argument version of error to be bad practice in any real code. You should use always use both a 'MSGID' and a 'ERRMSG' when writing errors for your functions, e.g.
error('FunctionName:SubFunctionName:ErrorMSGID','Error message to be printed.')
Alternatively, you can use MException objects in conjuction with throw, rethrow, and throwAsCaller, which allow you to reuse error information. More here.
It is odd, but it's in the documentation for error, for the error('msgString') syntax:
All string input arguments must be enclosed in single quotation marks. If msgString is an empty string, the error command has no effect.
Similarly, if using the error(msgStruct) syntax:
If msgStruct is an empty structure, no action is taken and error returns without exiting the function.
if you have a look to the try documentation you can have an example.
Else want you want for your code it :
M = 3;
j = 1;
k = [Inf, 5, 4];
while M>0
try
M = M-1
u = k(j)
if (isinf(u)||isnan(u)), error(''), end;
catch
disp('I catch an error!');
j = j+1
end
end
Because If you never get an error in your code, it will never go in the catch. So by including error('');, it just to say, go execute the statement in the catch.
But you can just modify your code by replacing the error() by the statements into your catch like this :
while M>0
M = M-1
u = k(j)
if (isinf(u)||isnan(u)), j = j+1, end;
end
EDIT
If you take a look in the documentation, you can found this :
% ERROR(MSGSTRUCT) reports the error using fields stored in the scalar
% structure MSGSTRUCT. This structure can contain these fields:
%
% message - Error message string
% identifier - See MESSAGE IDENTIFIERS, below
% stack - Struct similar to the output of the DBSTACK function
%
% If MSGSTRUCT is an empty structure, no action is taken and ERROR
% returns without exiting the program. If you do not specify the
% stack, the ERROR function determines it from the current file and line.
So no action is taken as you can read. And nothing, so catch don't get any informations.
Not sure why you need it, but here is how it works.
error function does not throw an error with empty string or empty vector ([]) as an argument.
If you don't specify argument at all the error function itself generates the error "Not enough arguments". So it will go to catch.
Another way is to specify an empty structure as an argument.
s = struct();
error(s)
In this case, the error will be generated, but the code will not stop and in general flow you will hear no beep. In your case it should go to catch.
temp(i,1) = rand(1)*(pb(1,num).pos(i,1) - pw(1,num).pos(i,1));
This line gives the following error:
Error using ==> minus
Not enough input arguments.
The following are the definitions of pb and pw.
pw=struct('fitness',[],'pos',{});
pb=struct('fitness',[],'pos',{});
pos is a 2 x 1 array.
When tracking down errors like this, I break the problem up into smaller bits. Especially when the logic isn't readily apparent. Not only does it provide a path that can be used to step through your function using the debugger, but it also makes it more readable.
I've taken liberty with the intermediate variable names.
thisPb = pb(1,num);
thisPw = pw(1,num);
initialPos= pw.pos(i,1);
finalPos = pb.pos(i,1);
whos initialPos finalPos
temp(i,1) = rand(1) * (finalPos - initialPos);
The line with whos will print out the values. Make sure that finalPos and initialPos are both numbers.
One way that you can get this error is when num is an empty matrix.
The expression
>> s(x).a
can return a variable number of outputs, depending on the size of x.
If x = [1,2,3] for example, it will return three values (as long as s has at least three elements).
If x = [] on the other hand, then s(x).a will return no outputs, so the expression
>> disp(s(x).a)
will give you a Not enough input arguments error, which is almost certainly what you're seeing. You should check that num is not empty.
Are you sure, that all values are really initialised? Try to check this before your codeline.
disp(pb(1,num).pos(i,1))
disp(pw(1,num).pos(i,1))
temp(i,1) = rand(1)*(pb(1,num).pos(i,1) - pw(1,num).pos(i,1));