Explore BrainMass
Share

# find the errors in the MATLAB codes

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

The program runs, then stops and gives an error of double going into the switch. I changed the code to make it output the sounds, but my whole reasoning was to make this version work with the row vectors.

%taking input phone numbers

Horvector=[1209 1336 1477];
Vervector=[697, 770, 852, 941];

x=input('Enter Phone Number: ','s');

a=0.5;
t=0:1/8192:0.5;
for i=1:length(x)
y=z(i);
switch y

case '1'
z(i)=a*sin(2*pi*Horvector(1)*t) + a*sin(2*pi*Vervector(1)*t);
case '2'
z(i)=a*sin(2*pi*Horvector(2)*t) + a*sin(2*pi*Vervector(1)*t);
case '3'
z(i)=a*sin(2*pi*Horvector(3)*t) + a*sin(2*pi*Vervector(1)*t);
case '4'
z(i)=a*sin(2*pi*Horvector(1)*t) + a*sin(2*pi*Vervector(4)*t);
case '5'
z(i)=a*sin(2*pi*Horvector(2)*t) + a*sin(2*pi*Vervector(2)*t);
case '6'
z(i)=a*sin(2*pi*Horvector(1)*t) + a*sin(2*pi*Vervector(2)*t);
case '7'
z(i)=a*sin(2*pi*Horvector(1)*t) + a*sin(2*pi*Vervector(3)*t);
case '8'
z(i)=a*sin(2*pi*Horvector(2)*t) + a*sin(2*pi*Vervector(3)*t);
case '9'
z(i)=a*sin(2*pi*Horvector(3)*t) + a*sin(2*pi*Vervector(3)*t);
case '*'
z(i)=a*sin(2*pi*Horvector(1)*t) + a*sin(2*pi*Vervector(4)*t);
case '0'
z(i)=a*sin(2*pi*Horvector(2)*t) + a*sin(2*pi*Vervector(4)*t);
case '#'
z(i)=a*sin(2*pi*Horvector(3)*t) + a*sin(2*pi*Vervector(4)*t);

otherwise

disp('Enter a valid number or * or #')

end
end

%to listen to the tones with a 0.25 sec pause between each tone

for i=1:length(x)
sound(z(i));
pause(0.25);
end

https://brainmass.com/computer-science/matlab/find-errors-matlab-codes-100740

#### Solution Preview

First of all, you have y=z(i); in you attached codes, which should be y=x(i);

Secondly, ...

#### Solution Summary

It finds and corrects the errors in the Matlab codes. The response was rated '5/5' by the student who posted the question originally.

\$2.19

## MATLAB Code Debugging

Without changing the codes, the errors boxed is what I am getting when I run these codes. I am not sure if I am supposed to create files first. I would like codes that are basic and running.

function out=bisect_1(fname, a, b, eps)
% bisection algorithm
% fname must be a function handle (if you want to use a string instead,
% use eval)
% sign(fa)*sign(fb) must be negative where fa=feval(fname,a) and
% fb=feval(fname,b)
%
% Example:
% fname=@(x)(exp(-cos(x))-1);
% root1=bisect_1(fname, 1, 2, 1e-3);
% root1
%
% To find the second root, use a=4 and b=5
% To find the third root, use a=7 and b=8
%
% Now to compare the results, use fzero() function
% first root-->fzero(fname, 1)
% second root-->fzero(fname, 5)
% third root-->fzero(fname, 8)
%
% You could add more codes to check the inputs to the function
% and act accordingly.
%
if sign(feval(fname,a))*sign(feval(fname,b)) > 0
disp('function doesn''t cross zero betwenn the specified interval!!');
return ;
end
N=100; %to avoid infinite loop
found=false;
iter=0;
while(iter<N)
iter=iter+1;
c=(a+b)/2;
fa=feval(fname,a);
fb=feval(fname,b);
fc=feval(fname,c);
if (fc==0 || (b-a)/2<eps)
out=c;
found=true;
break;
else
if sign(fc)==sign(fa)
a=c;
else
b=c;
end
end
end
if found==false
end

The error Iam getting for the above code
??? Input argument "fname" is undefined.

Error in ==> bisect_1 at 25
if sign(feval(fname,a))*sign(feval(fname,b)) > 0

function out=insertionSort(inArray)
% insertion sort algorithm
% inArray must be a vector
% out is the result of insertion sort
% Example:
% a=round(10*rand(1,10)+1);
% a_sorted=insertionSort(a);
% a_sorted
%

for i=1:length(inArray)
temp=inArray(i);
j=i;
while (j>1 && inArray(j-1)>temp)
inArray(j)=inArray(j-1);
j=j-1;
end
inArray(j)=temp;
end
out=inArray;

??? Input argument "inArray" is undefined.

Error in ==> insertionSort at 11
for i=1:length(inArray)

function out=my_cos(x, N, varargin)

%check for eps
if length(varargin)>1
disp('number of arguments must be 2 or 3!!')
return;
elseif length(varargin)==1
eps=varargin{1};
else
eps=0;
end

%start estimation
result=0;
for i=0:N-1
%calculate i-th term
numerator=(-1)^i;
denom=factorial(2*i);
factor=x^(2*i);
temp=(numerator/denom)*factor;
%compare to eps
if abs(temp)>eps
result=result+temp;
else
break;
end
end
out=result;

error for the above code
??? Input argument "N" is undefined.

Error in ==> my_cos at 15
for i=0:N-1

View Full Posting Details