Can't pass a function as an argument Matlab - 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

Related

NULLSPACE - RREF Command in Matlab bugs

Can someone help me debug this Matlab code? Like I have a matrix A, and I need to find A^k by diagonalization method. Below is my original code, but there is a problem with this part:
m = (M - R(i,1)*eye(NumRowsR));
disp(rref(m));
t = null(rref(m));
disp(t);
This part can't give me the nullspace of matrix t after reduced for some reasons (I recently did some research on the Internet and see some people said about the bug of rref and null). The problem is that it keeps showing me elementary matrix
1 0 0
0 1 0
0 0 1
for the eigenvalues. Does anyone know how to fix it? I will be very much appreciated!
Below is my original code, for a better view:
syms x NumRowsM NumColsM NumRowsR NumColsR NumRowst NumColst k numeigen
M = input('Input a square matrix M: ');
k = input('Input the power of matrix M: ');
[NumRowsM, NumColsM]=size(M);
if (NumRowsM ~= NumColsM)
disp('Not valid input. Matrix must be a square matrix!')
else
%Find eigenvalues:
R = solve(det(M - x*eye(NumRowsM)), x);
[NumRowsR, NumColsR] = size(R);
if (or(NumRowsR == 0,NumColsR == 0))
disp('No eigenvalues. The matrix is not diagonalizable')
else
numeigen = 0;
F = zeros(NumRowsR, NumRowsR);
d = zeros(NumRowsR,1);
for i = 1:NumRowsR
m = (M - R(i,1)*eye(NumRowsR));
disp(rref(m));
t = null(rref(m));
disp(t);
[NumRowst, NumColst] = size(t);
if (NumColst == 0)
if (i == NumRowsR && numeigen > NumRowsR)
disp('Matrix not diagonalizable due to invalid eigenvalue');
return
else
continue
end
else
numeigen = numeigen + 1;
if (NumColst == 1)
for j = 1:NumRowsR
[n, d(j)] = numden(sym(t(j,1)));
end
for j = 1:NumRowsR
F(j,i) = sym(t(j,1)) * lcm(sym(d));
end
else
for k = 1:NumColst
for j = 1:NumRowsR
[n, d(j)] = numden(sym(t(j,k)));
end
for j = 1:NumRowsR
F(j,k) = sym(t(j,k)) * lcm(sym(d));
end
end
end
end
end
disp(F);
D = (F\M)*F;
disp('The power k of the matrix M is: ');
T = F*(D^k)/(F);
disp(T)
end
end

"Inner matrix dimensions must agree" - but it shouldn't be a matrix

The following is a snippet from a function I am defining:
function [V1, V2] = lambert(R1, R2, t, string)
r1 = norm(R1);
r2 = norm(R2);
z = -100;
while F(z,t) < 0
z = z + 0.1;
end
tol = 1.e-8;
nmax = 5000;
ratio = 1;
n = 0;
while (abs(ratio) > tol) & (n <= nmax)
n = n + 1;
ratio = F(z,t)/dFdz(z);
z = z - ratio;
end
f = 1 - y(z)/r1;
g = A*sqrt(y(z)/mu);
gdot = 1 - y(z)/r2;
V1 = (R2-f*R1)/g;
V2 = (gdot*R1-R1)/g;
function dum = y(z)
dum = r1 + r2 + A*(z*S(z) - 1)/sqrt(C(z));
end
When I run this code with some vectors R1, R2, I get:
Error using *.
Inner matrix dimensions must agree.
This occurs at these lines:
V1 = (R2-f*R1)/g;
V2 = (gdot*R1-R1)/g;
I think this is due to the fact that f and gdot and g are matrices whose dimensions are incompatible with R1 and R2. However, as I have defined them, they should simply be scalars. I do not understand what in the code causes them to be matrices.
I've tried using .* instead of * etc, but with no success.

How can I get a proper double form for a symbolic matrix?

How can I get a proper double form from this symbolic Matrix in MatLab? I've tried everything but I prefer not using feval or inline function as they're not recommended
This is the code to get the matrix
function T = Romberg (a, b, m, f)
T = zeros(m, m);
T = sym(T);
syms f(x) c h;
f(x) = f;
c = (subs(f,a)+subs(f,b)) / 2;
h = b - a;
T(1,1) = h * c;
som = 0 ;
n = 2;
for i = 2 : m
h = h / 2;
for r = 1 : n/2
som = som + subs(f,(a + 2*(r-1)*h));
T(i,1) = h * (c + som);
n = 2*n;
end
end
r = 1;
for j = 2 : m
r = 4*r;
for i = j : m
T(i,j) = (r * T(i, j-1) - T(i-1,j-1)/(r-1));
end
end
end
And with an input like this
Romberg(0, 1, 4, '2*x')
I get a symbolic matrix output with all the
3 * f(3)/2 + f(1)/2 + f(5)/2
I would like to have a double output.
Can you please help me?
Thank you very much in advance!

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

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.

Subscript indices must either be real positive integers or logicals

i have the following error in my matlab function code:
??? Subscript indices must either be real positive integers or logicals.
Error in ==> AFA at 15
M(k,j) = mean(T(i:sze,j));
here is the part of the code where the problem is :
sz =size(T);
lim = sz(2) - ordre;
M = zeros(sz(1),sz(2));
r= 0;
for j=1:sze,
k = 1;
for i=1:lim,
M(k,j) = mean(T(i:i+ordre,j));
k = k + 1;
end
for i=lim+1:sz(2),
M(k,j) = mean(T(i:sz(2),j));
k = k + 1;
end
end
The following works for me without error
T = magic(25);ordre = 5; %# I make up some values here
sz =size(T);
lim = sz(2) - ordre;
%# I've added a check here
if lim < 1, error('ordre has to be at most sz(2)-1 (is %i)',ordre);end
M = zeros(sz(1),sz(2));
r= 0;
for j=1:sz(2), %# I needed to change this line (sz(1) works as well)
k = 1;
for i=1:lim,
M(k,j) = mean(T(i:i+ordre,j));
k = k + 1;
end
for i=lim+1:sz(2),
M(k,j) = mean(T(i:sz(2),j));
k = k + 1;
end
end