unexpected matlab expression very simple code. - matlab

There seems to be a small problem with my matlabcode.
i'm trying to calculate Qx using this simple formula.
Anybody has an idea what i'm doing wrong?
Error: File: functie5612.m Line: 2 Column: 28
Unexpected MATLAB expression.
Error in oef5612 (line 2)
Qx=functie5612(D)
Define my function
function Qx=functie5612(D)
Qx= D*(11-(0.1*D)/(0.28-D))0.8
end
Initial parameter
D=[0;2;4;6;8;10;12;14;16;18;20;22;23;24;25;26;27;28;30;32;34;36;38]
Using my function
Qx=functie5612(D)
making a graph
clf
figure(1);
plot(D,Qx);
title ('Optimale dilutiesnelheid','FontSize',12);
xlabel('D(1/h)','FontSize',12);
ylabel('Volumetrische biomassaproductiviteit(kg/(m^3*h)','FontSize',12);
legend('Substraat','Product','Biomassa') `

You need the explicit * when doing multiplication. That is, you should have )*0.8 and not just )0.8.
So your function should look like:
function Qx=functie5612(D)
Qx= D*(11-(0.1*D)/(0.28-D))*0.8
end
However, this is still incorrect (dimensions mismatch). If you are looking at elementwise multiplication of D, you will need to use the . operator. The code would look like:
Qx= D.*(11-(0.1*D)./(0.28-D))*0.8

The error you get is due to a matrix dimesion mismatch.
So, you need to use the .* operator instead of *
Qx= D.*(11-(0.1.*D)./(0.28-D)).*0.8;

Related

Using fzero to solve eqn in MatLab

I hope this is the right area. I'm trying to get this code to work in MatLab.
function y=test(x)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
I then jump to the command value and type this:
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
I then try to find the zeros of the first equation by typing this and I get errors:
solution=fzero(#test,5000000)
I'm getting the following error:
Error: File: test.m Line: 5 Column: 1 This statement is not
inside any function. (It follows the END that terminates the
definition of the function "test".)
New error
Error using fzero (line 289)
FZERO cannot continue because user supplied function_handle ==> #(x)
(test(x,B,b,a,r,p))
failed with the error below.
Subscript indices must either be real positive integers or logicals.
I would guess that this is a problem of scoping, you are defining variables (B, b, etc...) in the command line but trying to use them inside your test function where they are out of scope. You should alter your test function to take these in as parameters and then use an anonymous function so that your call to test in fsolve still only takes a single parameter:
function y=test(x, B, b, r, a, p)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
and
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
solution=fzero(#(x)(test(x,B,b,a,r,p)),5000000)
As an aside, unless you really do mean matrix multiplication, I would suggest that you replace all your *s and /s in test with the element-wise operators .* and ./. If you are dealing with scalars, it doesn't matter now, but it makes a big difference if you later want to scale your project and need a vectorized solution.
Regarding the errors you have added to your question:
You can't put code after the end in your function file. (With the exception of local functions). Your objective function should be an .m-file containing the code for one single function.
This is because in your test function you have ...b((1-(b/x)^(B-1))... which in MATLAB means you are trying to index the variable b in which case the value of (1-(b/x)^(B-1) has to be a positive integer. I'm guess you are missing a *
Your
function y=test(x)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
cannot access variables in your workspace. You need to pass the values in somehow. You could do something like:
function y=test(x,B,b,a,r,p)
y=-x+(B/(B-1))*(r-a)*p+(B/(B-1))*(r-a)*(b((1-(b/x)^(B-1))/r- a)+p* ((b/x)^B))/(1-(b/x)^B);
end
and then you can create an implicit wrapper function:
B=3.0515;
b=1.18632*10^5;
a=.017;
r=.054;
p=5931617;
solution = fzero(#(x) test(x,B,b,a,r,p),5000000)
I haven't tested whether fzero returns sensible results, but this code shouldn't give an error.

Error regarding inlineeval in MATLAB

As part of a group project we have a system of 2 non linear differential equations and we have to draw the S=S(t) , I=I(t) graphic using the midpoint method.
And I'm getting the following error when trying to insert the matrix with the corresponding differential equations:
"Error in inline expression ==> matrix([[-(IS)/1000], [(IS)/1000 - (3*I)/10]])
Undefined function 'matrix' for input arguments of type 'double'.
Error in inline/subsref (line 23)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);"
The code I have done is the following:
syms I S
u=[S;I];
F=[-0.001*S*I;0.001*S*I-0.3*I];
F1=inline(char(F),'I','S');
h=100; %Valores aleatórios
T=100000;
ni=(T/h);
u0=[799;1];
f=zeros(1,2);
k=zeros(1,2);
i=1;
while i<=ni
f(1)=F1(u0(1));
f(2)=F1(u0(2));
dx=h*f;
k(1)=F1((u0(1)+h*(1/2)),(u0(2)+h*(1/2)));
k(2)=F1((u0(1)+h*(1/2)),(u0(2)+h*(1/2)));
u1=u0+h*k;
disp('i:'),disp(i)
disp('u= '),disp(u1)
u0=u1;
i=i+1;
end
I'm new to this so the algorithm it's very likely to be wrong but if someone could help me with that error I'd apreciate it. Thank you!
The problem that specifically creates the error is that you are putting two symbolic functions into a matrix and then calling char (which outputs matrix([[-(IS)/1000], [(IS)/1000 - (3*I)/10]]) rather than converting nicely to string).
The secondary problem is that you are trying to pass two functions simultaneously to inline. inline creates a single function from a string (and using anonymous functions instead of inline is preferred anyway). You cannot put multiple functions in it.
You don't need sym here. In fact, avoid it (more trouble than it's worth) if you don't need to manipulate the equations at all. A common method is to create a cell array:
F{1} = #(I,S) -0.001*S*I;
F{2} = #(I,S) 0.001*S*I-0.3*I;
You can then pass in I and S as so:
F{1}(500,500)
Note that both your functions include both I and S, so they are always necessary. Reconsider what you were expecting when passing only one variable like this: f(1)=F1(u0(1));, because that will also give an error.

Matlab fir1 function error

I'm running Matlab 2014a on Linux and trying to apply a simple FIR filter using the fir1 function. I keep getting the following error, no matter how I try to build the filter:
>>fir1(15,[0.1])
Error using *
Inner matrix dimensions must agree.
>>Error in firls (line 80)
cos_ints = [omega; sin((1:N)' * omega)];
>>Error in fir1 (line 121)
hh = firls(L-1,ff,aa);
I've used the debugger to go to the line of code, and it looks like it's always trying to multiply a column vector of length(order), (1:N)', by another column vector, omega. This doesn't make any sense. Is the fir1 function broken, or am I doing something wrong? This error occurs for me even if I try to run the examples given by MathWorks.
I would guess that Matlab's firls function is masked by another function of the same name, which is in Matlab's path and therefore gets called from fir1.
What do you get when you type :
which firls
? - You should get something which ends in \toolbox\signal\signal\firls.m

How to call a custom function with exponentiation for every element of a matrix in GNU Octave?

Trying to find a way to call the exponentiation function ( ^ ) used in a custom function for every item in a matrix in GNU Octave.
I am quite a beginner, and I suppose that this is very simple, but I can't get it to work.
The code looks like this:
function result = the_function(the_val)
result = (the_val - 5) ^ 2
endfunction
I have tried to call it like this:
>> A = [1,2,3];
>> the_function(A);
>> arrayfun(#the_function, A);
>> A .#the_function 2;
None of these have worked (the last one I believe is simply not correct syntax), throwing the error:
error: for A^b, A must be a square matrix
This, I guess, means it is trying to square the matrix, not the elements inside of it.
How should I do this?
Thanks very much!
It is correct to call the function as the_function(A), but you have to make sure the function can handle a vector input. As you say, (the_val - 5)^2 tries to square the matrix (and it thus gives an error if the_val is not square). To compute an element-wise power you use .^ instead of ^.
So: in the definition of your function, you need to change
result = (the_val-5)^2;
to
result = (the_val-5).^2;
As an additional note, since your code as it stands does work with scalar inputs, you could also use the arrayfun approach. The correct syntax would be (remove the #):
arrayfun(the_function, A)
However, using arrayfun is usually slower than defining your function such that it works directly with vector inputs (or "vectorizing" it). So, whenever possible, vectorize your function. That's what my .^suggestion above does.

"Not enough input arguments." when using fsolve with nested functions

I am trying to make my own function in matlab to solve for a system of two nonlinear equations, while using a nested function to share some some parameters, here is a sample code:
function y=solve(a,x0)
a;
y=fsolve(nle,x0); % this is line 3
function f=nle(x)
f(1)=x(1)-a*x(1)^2-x(1)*x(2); % this is line 6
f(2)=2*x(2)-x(2)+3*x(1)*x(2);
end
end
Here a is the parameter I want to pass from command line to the function, and x0 is the start point for the fsolve.
However, when I call the function in malab after specifying a=4 and x0=[1 1]', it gave me the following error:
Error using solve/nle (line 6)
Not enough input arguments.
Error in solve (line 3)
y=fsolve(nle,x0);
I'm quite a newbie for matlab, can anybody tell me where I am doing wrong?
Thanks in advance.
EDIT:
I tried substituting the nle with a function handle #nle, but seems something else went wrong:
Undefined function 'fsolve' for input arguments of type 'function_handle'.
Error in solve (line 3)
y=fsolve(#nle,x0);
Doesn't seem to make sense since I checked the documentation for fsolve, and it says it should indeed use a function handle there...
You miss the '#' in front of nle, i.e.
y = fsolve(#nle,x0);
should work.