Keep getting error with sample code

조회 수: 5 (최근 30일)
Stanley Zhu
Stanley Zhu 2021년 7월 17일
댓글: Stanley Zhu 2021년 7월 17일
So the commented up code was the one that was provided to essentially plot my part, yet I keep getting these errors: Undefined function 'number_of_solutions' for input arguments of type'double'.
Error in Lab4 (line 68)
n = number_of_solutions(A(m), b(m));
Not sure why I'm getting these errors, are there any toolboxes or something that I'm missing. Thanks.
clear, clc, close all
syms m f
A = @(m) [m -1; (3*m-m^3) (1-3*m^2)];
b = @(m) 2*(1-m) * [1; (m^2+4*m +1)];
det(A(m));
f = matlabFunction(det(A(m)));
eqn = f(m) == 0;
solve(eqn)error wit
dx = 0.05; dy = 0.25;
for m = -2.6 : 0.2 : 2.6
n = number_of_solutions(A(m), b(m));
if f(m) == 0
zero_marker = plot(m, 0, 'bo', 'MarkerSize', 16, 'MarkerFaceColor', 'white');
else
plot(m, f(m), 'bh', 'MarkerSize', 10, 'MarkerFaceColor', 'green')
end
the_text = text(m+dx, f(m) + dy, num2str(n), 'FontSize', 24) ;
the_text.set('FontWeight', 'bold', 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
% Auto Color Code the Zero Markers and the Text
if n==0 % inconsistent! No solutions
the_text.set('Color', 'red')
% Add code to set the MarkerFaceColor for zero_marker to red.
set(zero_marker, {'MarkerFaceColor'}, get(zero_marker, 'red'));
elseif n==inf % there are free variables
the_text.set('string', '\infty')
% Add code to set the MarkerFaceColor for zero_marker to yellow.
set(zero_marker, {'MarkerFaceColor'}, get(zero_marker, 'Yellow'));
else
the_text.set('Color', 'green') % unique solution!
end
end

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 7월 17일
편집: Cris LaPierre 2021년 7월 17일
Where was this coded up? It looks like a homework assignment from Drexel University (Engr 231).
The issue is that number_of_solutions is not a matlab function, and your code does not create it either. The result is an error because MATLAB can't find it. Perhaps you were provided with additional code you are overlooking?
A = number_of_solutions(4,5)
Unrecognized function or variable 'number_of_solutions'.
  댓글 수: 2
Stanley Zhu
Stanley Zhu 2021년 7월 17일
Yes it's a lab assignment from Drexel, I forgot to add in that I did also create a new function file and pasted this code in as instructed. I'm not certain what I'm getting wrong as it's still the same issue.
function [number] = number_of_solutions(A,b)
% Input A is an mxn matrix. b is a mx1 column vector.
% The unknown x is an nx1 column vector.
% Finds the number of solutions of the linear system Ax = b
% Number of solutions can only be
% 0. zero, (inconsistent)
% 1. one (unique) or
% inf. infinite (there are free variables).
% (Infinity in MATLAB is written inf or Inf.)
number_of_variables = numel( A(1,:)); % x1, x2, ..., xn
AM = [A b]; % augmented matrix
if rank(AM) > rank(A) % system is inconsistent. There is a pivot in the final column.
number = 0;
elseif rank(A) < number_of_variables
number = inf; % There are free variables
else
number = 1; % unique!
end
end
Stanley Zhu
Stanley Zhu 2021년 7월 17일
Ok nevermind it seems I goofed up in terms of where I created the function folder, should've definitely had some rest cause I completely skipped over the line. Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by