Error: `Input argument "N" is undefined`, in simple matlab program - matlab

I have this tiny program in Matlab.
laMatriz.m
function k = laMatriz(X)
Y = 9;
A = zeros(X, Y);
for i=1:X
V = elVector(Y);
LimY = length(elVector);
for j=1:LimY
A(i,j) = V(j);
end
end
k = A;
end
elVector.m
function elVector = elVector(N)
%fprintf('largo de elVector %i\n', N);
elVector=1:N;
end
Calling function laMatriz(10) results in this error:
??? Input argument "N" is undefined.
Error in ==> elVector at 3
elVector=1:N;
Error in ==> laMatriz at 11
LimY = length(elVector);
why? how can i fix it?

The problem is in this function:
function k = laMatriz(X)
Y = 9;
A = zeros(X, Y);
for i=1:X
V = elVector(Y);
LimY = length(elVector); <-- here you are calling length(elVector)
for j=1:LimY
A(i,j) = V(j);
end
end
k = A;
end
elVector is a function so you cannot call length with it. Did you mean length(V)? Basically your error is saying the argument N to the function elVector doesn't exist.

Related

Can't pass a function as an argument Matlab

i'm studing matlab and I get some problem with a code.
First, I've created f1.m:
function y = f1(x)
y = exp(x) - pi;
end
Then, I built a code to find the roots by bisector method:
function [root, err, n] = bissect(f, a, b, errMax)
m = (a+b)/2;
err = (b-a)/2;
n = 0;
while err > errMax
if f(a)*f(m) > 0
a = m;
else
b = m;
end
m = (a+b)/2;
err = (b-a)/2;
n = n + 1;
end
root = m;
end
But, when i run
>> [r,err,n] = bissect(#f1, 1, 2, 0.1);
it returns:
Warning: Subscript indices must be integer values.
In C:\matlabR12\work\Codigo\bissect.m at line 12
??? Index exceeds matrix dimensions.
Error in ==> C:\matlabR12\work\Codigo\bissect.m
On line 12 ==> if f(a)*f(m) > 0
What am I doing wrong?
I'm using Matlab R12

Integrating over a constant function

I am trying to integrate over a constant function in MATLAB 2017a, but I am stuck. First of all when I integrate using the following script, I get the right output. So the script works for a x0 which depends on t.
function E=sol(n,k)
x0 = #(t) t^(2);
j = 0;
E = zeros(n,1);
while j < n+1 ;
K = matlabFunction(subs(po(j,k))) ;
eval(sprintf('x%d = integral(K,0,1);',j+1)) ;
E(j+1,1) = subs(sprintf('x%d',j+1))
j = j+1;
end
end
Where function po(j,k) is as follows,
function A_j = po(j,k) % Adomian polynomials
if j >0
x = sym('x',[1 j]);
syms p; % Assinging a symbolic variable for p
syms x0;
S = x0+ sum(p.^(1:j) .* x) ; % Sum of p*x up to order j
Q =f(S,k); % Taking the k-th power of S, i.e.
A_nc = diff(Q,p,j)/factorial(j); % Taking the j-th order derivative
A_j = subs(A_nc,p,0) ; % Filling in p=0
else
syms x0;
S = x0;
A_j =f(S,k); % Taking the k-th power of S,
end
end
And where f(x,k) is,
function F = f(x,k) % Nonlinear function of k power
F = x^k ;
end
Now when I cal sol(n,k) it does work. But when I try to change my x0 function in sol(n,k) in a constant function like,
function E=solcon(n,k)
x0 = #(t) 2.*ones(size(t));
j = 0;
E = zeros(n,1);
while j < n+1 ;
K = matlabFunction(subs(po(j,k))) ;
eval(sprintf('x%d = integral(K,0,1);',j+1)) ;
E(j+1,1) = subs(sprintf('x%d',j+1))
j = j+1;
end
end
It does not work, As you can see I added *ones(size(t)); just to make it a function of t. But unfortunately it still doesn't work when I call,
K = matlabFunction(subs(po(j,k))) ;
I get,
#()4.0
And then I get an error when I call,
eval(sprintf('x%d = integral(K,0,1);',j+1))
Could anyone help me out trying to integrate over a constant?
The error I get when I call solcon(10,2) is
Error using symengine>#()4.0
Too many input arguments.
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in solcon1 (line 7)
eval(sprintf('x%d = integral(K,0,1);',j+1)) ;
EDIT 2
I used the following script,
function E=solcon(n,k)
x0 = #(t) 2.*ones(size(t));
j = 0;
E = zeros(n,1);
while j < n+1 ;
K = matlabFunction(subs(po(j,k))) ;
fstr= func2str(K)
if fstr(3) == ')';
x{j+1} = K*(1-0)
else x{j+1} = integral(K,0,1)
end
E(j+1,1) = subs(x{j+1},1);
j = j+1
end
end
But the following error occurs,
Undefined operator '*' for input arguments of type 'function_handle'.
Error in solcone1 (line 9)
x{j+1} = K*(1-0);
I am going to ignore the terrible choice of using eval, especially when you can do
x{j+1} = integral(K,0,1);
Read more on why dynamic variables and eval are terrible
Your problem is that matlabFunction is a smartass. When it detects that your function does not have any dependency to x, it gives you a function with empty input arguments #()4.0. As you see, integral does not like that.
A way of solving the problem is detecting this before calling integral. You could check if it has input arguments, and if it does not, then evaluate the integral "by hand"
...
while j < n+1 ;
K = matlabFunction(subs(po(j,k))) ;
fstr=func2str(K);
if fstr(3)==')'
x{j+1}=K()*(1-0); % evaluate the integral yourself
else
x{j+1} = integral(K,0,1);
end
E(j+1,1) = subs(x{j+1});
j = j+1;
end
...
The problem is considerably more difficult that I though I was. Either rewrite the entire thing, or using eval:
...
while j < n+1 ;
K = matlabFunction(subs(po(j,k))) ;
fstr=func2str(K);
if j==0
eval(sprintf('x%d = K()*(1-0);;',j+1)) ;
else
eval(sprintf('x%d = integral(K,0,1);',j+1)) ;
end
E(j+1,1) = subs(sprintf('x%d',j+1));
j = j+1;
end
...

how to evaluate a derivative of a function in matlab in one point? Error "dimensions must agree"

I have a function called funcion(x) which does:
function fx = funcion(x)
fx = cos(x); %%
return;
then in my other function (they are both in the same script) called newtonRaph i'm doing this:
function raiz = newtonRaph(xi,imax, tol)
syms x;
iter = 0;
xold = xi;
x = xold;
df = diff(cos(x),x);
er = 0.9;
while (er>tol)&&(iter<imax)
dfr = (subs(df,x,xold));
nuevo = 0.222/dfr;
if(dfr ==0)
disp('dfr was 0...');
break;
else
iter = iter+1;
evaluacion = funcion(x);
xnew = xold - (evaluacion/dfr); %Newton-Raphson formula
if(xnew~=0)&& (iter>1)
er = abs((xnew-xold)/xnew); %
end
xold = xnew;
x = xold;
end
end
root = xnew;
As you can see, i added a test line that does new = 0.222/dfr just to try and see what's happening with the derivative.
I don't know what is it that i'm doing wrong, but every time i run this, it tells me
??? Error using ==> mldivide
Matrix dimensions must agree.
Error in ==> newtonRaph at 16
nuevo = 0.222/dfr;
I would be really thankful if anyone could tell me what to do.
If dfr is not a scalar, and you only want to divide 0.222 by all the elements in it, then you should write nuevo = 0.222./dfr with a . before the /.

Matlab Error using mupadmex

I'm trying to compile this matlab code,
`
lambda = 1/pi;
delta = 3;
alpha = 3.5;
sum = 0;
syms t;
for k = 1:5
for l = 0:(k*delta)
for m = 1:2
a = ((lambda*pi)^(l+(alpha/2)+(m*delta)+1))/(factorial(l)*factorial((k*delta) -1)*factorial(m*delta));
gamma = # (t) exp(-t).*t.^((k*delta)-l-(alpha/2));
f = # (u) int(gamma,t,u,inf);
g = #(u) u.^((2*l) - alpha + (2*m*delta+1));
h = #(u) a.*g(u).*f(u);
b = integral(#(u) h(u),0,inf);
sum = sum +b ;
end
end
end
sum
I get this error,
Error using mupadmex
Error in MuPAD command: The argument is invalid. [Dom::Interval::new]
anyone have an idea how to solve this problem ?
Many thanks,

variable in solving the equation

I want to solve equations in matlab, eg.
100+a/2=173*cos(b)
sqrt(3)*a/2=173*sin(b)
and the code would be:
[a,b]=solve('100+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')
However, if I want to take 100 as a variable, like
for k=1:100
[a,b]=solve('k+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')
end
There would be an error, how to make it?
degree=140/1000000;
p=42164000;
a=6378136.5;
b=6356751.8;
x_1=0;
y_1=p;
z_1=0;
for i=451:550
for j=451:550
alpha=(1145-i)*degree;
beta=(1145-j)*degree;
x_2=p/cos(alpha)*tan(beta);
y_2=0;
z_2=p*tan(alpha);
syms x y z x_1 x_2 y_1 y_2 z_1 z_2 a b
eq = [(x-x_1)*(y2-y_1)-(x_2-x_1)*(y-y_1),(x-x_1)*(z_2-z_1)-(x_2-x_1)*(z-z_1), b^2*(x^2+y^2)+a^2*(y^2)-a^2*b^2 ];
sol = solve(eq(1),x,eq(2),y, eq(3),z);
sol.x
sol.y
sol.z
end
end
I got the expression value, how do I get the numeric value of x,y,z?
[['x(1)=';'x(2)='],num2str(double(sol.x))]
not work ,shows
??? Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in ==> sym.sym>sym.double at 927
Xstr = mupadmex('mllib::double', S.s, 0);
Error in ==> f2 at 38
[['x(1)=';'x(2)='],num2str(double(sol.x))]
If you have access to the Symbolic Toolkit then you do the following:
syms a b k
eq = [k+a/2-173*cos(b), sqrt(3)*a/2-173*sin(b)];
sol = solve(eq(1),a,eq(2),b);
sol.a = simplify(sol.a);
sol.b = simplify(sol.b);
% There are two solutions for 'a' and 'b'
% check residuals for example k=20
subs(subs(eq,{a,b},{sol.a(1),sol.b(1)}),k,20)
% ans = 0.2e-13
subs(subs(eq,{a,b},{sol.a(2),sol.b(2)}),k,20)
% ans = 0.2e-13
Edit 1
Based on new code by OP the matlab script to solve this is:
clear all
clc
syms alpha beta
degree=140/1000000;
p=42164000;
a=6378136.5;
b=6356751.8;
x_1=0;
y_1=p;
z_1=0;
x_2 = p/cos(alpha)*tan(beta);
y_2 = 0;
z_2 = p*tan(alpha);
syms x y z
eq = [(x-x_1)*(y_2-y_1)-(x_2-x_1)*(y-y_1);...
(x-x_1)*(z_2-z_1)-(x_2-x_1)*(z-z_1); ...
b^2*(x^2+y^2)+a^2*(y^2)-a^2*b^2 ];
sol = solve(eq(1),x,eq(2),y,eq(3),z);
sol.x = simplify(sol.x);
sol.y = simplify(sol.y);
sol.z = simplify(sol.z);
pt_1 = [sol.x(1);sol.y(1);sol.z(1)] % First Solution Point
pt_2 = [sol.x(2);sol.y(2);sol.z(2)] % Second Solution Point
x = zeros(100,100);
y = zeros(100,100);
z = zeros(100,100);
for i=451:550
disp(['i=',num2str(i)])
for j=451:550
res = double(subs(pt_1,{alpha,beta},{(1145-i)*degree,(1145-j)*degree}));
x(i-450, j-450) = res(1);
y(i-450, j-450) = res(2);
z(i-450, j-450) = res(3);
end
end
disp('x=');
disp(x);
disp('y=');
disp(x);
disp('z=');
disp(x);
I would try
for i=1:100
k=num2str(i)
[a,b]=solve('100+a/2=173*cos(b)','sqrt(3)*a/2=173*sin(b)','a','b')
end
and then solve the equation