- makes the mistake less likely to happen in the first place.
- makes the mistake much easier to locate.
How do i fix the error in this code? I says "Illegal use of reserved keyword "case""
22 views (last 30 days)
Show older comments
clc
clear all
close all
m = true;
while m;
disp ('Extra Credit \n');
m = input ('Select which homework assignment to view: \n 1. Assignment 1 \n 2. Assignment 2 \n 3. Assignment 3 \n 4. Assignment 4 \n 5. Exit')
switch (m)
case 1
disp('Problem 1a');
(((35.7)*(64))-(7^3))/(45+(5^2))
disp('Problem 1b');
(5/4)*7*(6^2)+((3^7)/((9^3)-652))
disp('Problem 2a');
((2+7)^3)+(((273)^(2/3))/2)+((55^2)/3)
disp('Problem 2b');
(2^3)+(7^3)+((273^3)/2)+(55^(3/2))
disp('Problem 3a');
((cos((5*pi)/6)).^2)*((sin(7*pi/8)).^2)+((tan(((pi/6)*(log(8)))))/(sqrt(7)))
disp('Problem 3b');
(cos((5*pi)/(6)).^2)*((sin((7*pi)/(8))).^2)+((tan((pi/6)*(log(8)))/((7)*(5/2))))
disp('Problem 4a');
x=13.5
(x^3)+(5*(x^2))-(26.7*x)-(52)
disp('Problem 4b');
x=13.5
(sqrt(14*(x.^3)))/(exp(3*x))
disp('Problem 4c');
x=13.5
log((x.^2)-(x.^3))
disp('Problem 5a');
a=3
b=6
c=2
d=(.5)*((a*b)-(c))
disp('Problem 5b');
(d*(exp(d/2)))+((((a*d)+(c*d))/((20/a)+(30/b)))/(a+b+c+d))
disp('Problem 6');
a = [1,-2,-1,2];
roots(a)
disp('Problem 7');
x = 0:0.1:(2*pi);
y = (3*sin((1/2)*x));
figure(1),plot(x,y),xlabel('x'),ylabel('y')
disp('displaying in figure 1');
disp('Problem 8');
x = 0:0.1:(2*pi);
y = ((1/2)*cos(3*x));
figure(2),plot(x,y),xlabel('Radians'),ylabel('Amplitude')
disp('displaying 0.1 increment in figure 2');
x = 0:0.5:(2*pi);
y = ((1/2)*cos(3*x));
figure(3),plot(x,y),xlabel('Radians'),ylabel('Amplitude')
disp('displaying 0.5 increment in figure 3');
disp('Problem 9');
x = -1.2:0.062:5;
y = ((1/2)*cos(3*x))+ (3*(sin((1/2)*x)));
figure(4),plot(x,y),xlabel('x'),ylabel('y')
disp('displaying in figure 4');
disp('Problem 10');
A=[1,1,0;1,-3,0;2,1,3];
B=[6;7;15];
Solution = A/B
di
case 2
disp('Problem 1');
c = [32, 4, exp(2.5), 63, cos(pi/3), 14.12]
disp('Problem 2');
k = [55; 14; log(51); 987; 0; (5*(sin((2.5)*pi)))]
disp('Problem 3');
m = 15:-5:-25
disp('Problem 4');
Afirst = 4:3:49
r = Afirst(:,1:4);
w = Afirst(:,13:16);
Asecond = [r,w]
disp('Problem 5a');
A = [6, 43, 2, 11, 87; 12, 6, 34, 0, 5; 34, 18, 7, 41,9]
va = A(2,1:5)
disp('Problem 5b');
A = [6, 43, 2, 11, 87; 12, 6, 34, 0, 5; 34, 18, 7, 41,9]
vb = A(1:3,4)
disp('Problem 5c');
A = [6, 43, 2, 11, 87; 12, 6, 34, 0, 5; 34, 18, 7, 41,9]
vc = A(1:2,1:5)
disp('Problem 5d');
A = [6, 43, 2, 11, 87; 12, 6, 34, 0, 5; 34, 18, 7, 41,9]
b = A(:,2);
c = A(:,5);
vd = [b,c]
disp('Problem 6a');
a = [zeros(2,5)]
disp('Problem 6b');
b = [ones(0,1);eye(4)]
disp('Problem 6c');
c = [ones(1,2);ones(1,2);ones(1,2)]
disp('Problem 7a');
A = [ones(0,1);eye(7)]
disp('Problem 7b');
A = [ones(0,1);eye(7)]
disp('Problem 8');
x = -2.5:.5:3
y = ((1+x.^2).^3).*(x.^3)
disp('Problem 9');
h = .9
k = 12.5
y = .9, .8, .7, .6
z = 2.5, 3, 3.5, 4
T = ((x.*y.*z)/((h+k).^(k/5)))+((k.*(exp((z./x)+y)))/(z.^(h)))
disp('Problem 10a');
n = (0:1:50);
x = (1)./(((2.*n)+1).*((2.*n+2)))
sum((1)./(((2.*n)+1).*((2.*n+2))))
disp('Problem 10b');
n = (0:1:500);
x = (1)./(((2.*n)+1).*((2.*n+2)))
sum((1)./(((2.*n)+1).*((2.*n+2))))
disp('Problem 10c');
n = (0:1:5000);
x = (1)./(((2.*n)+1).*((2.*n+2)))
sum((1)./(((2.*n)+1).*((2.*n+2))))
case 3
clc
clear all
close all
disp('Problem 1');
x = -10:0.1:10
F = ((1.5).*x)./(x-4)
x1 = -10:0.1:3.7
x2 = 4.3:0.1:10
y1 = ((1.5).*x1)/(x1-4)
y2 = ((1.5).*x2)/(x2-4)
plot (x,F,x1,y1,x2,y2);
xlabel('X')
ylabel('Y')
title('Problem 1')
disp('Problem 2');
R1 = 1:1:10 % Load Resistance, Ohms
Vs = 12 % Voltage Source, Volts
Rs = 2.5 % Internal Resistance, Ohms
P = ((Vs.^2).*R1)./((R1+Rs).^2)
plot(R1,P);
xlabel('Low Resistance(Ohms)')
ylabel('Power(Watts)')
disp('Problem 3');
P = 5000;
T = 15;
R = .075;
M1 = 0:1:15;
M2 = 0:0.25:15;
V1 = P.*(1+(R./M1)).^(M1.*T)
V2 = P.*(1+(R./M2)).^(M2.*T)
V3 = P.*exp(R.*T)
figure(3),loglog(M1,V1,'g',M2,V2,'r--',M1,V3,'o')
legend('Annual Compound','Quarter Compound','Continuous Compound')
disp('Problem 4');
x = [0.00007, 0.0002, 0.05, 0.8, 4.2, 215, 3500];
y = [345, 362, 419, 454, 485, 633, 831];
yld = 350.*[((x./210).^0.16)+1];
figure
plot(x,y,'*',x,yld,'--');
set(gca, 'xscale','log');
set(gca, 'yscale','linear');
xlabel('Strain Rate')
ylabel('Yield Stress')
title('Strain and Yield')
legend('Coordinate for Strain Rate(x) and Yield Stress(y)', 'Function of Yield Stress', 'Location', 'Northwest')
case 4
disp('Problem 1');
x = [3 1 2 5 4]
find (x > 2)
y = x(x < 4)
disp('Problem 2');
a = [0,2,1;3,1,0;4,5,4;2,0,2]
a ==0
a(a >= 4)= -1
disp('Problem 3');
x = [5,-3,0,0,8]
y = [2,4,0,5,7]
z = x|y
z = x(x <= 0)
disp('Problem 4');
TNY = [31,26,30,33,33,39,41,41,34,33,45,42,36,39,37,45,43,36,41,37,32,32,35,42,38,33,40,37,36,51,50]
TANC = [37,24,28,25,21,28,46,37,36,20,24,31,34,40,43,36,34,41,42,35,38,36,35,33,42,42,37,26,20,25,31]
A = (sum(TNY))/31
B = (sum(TANC))/31
length(find(TNY < A))
length(find(TANC < B))
length(find(TANC > TNY))
find(TANC > TNY)
length(find(TANC == TNY))
find(TANC == TNY)
fprintf('\nAverage temperature for New York City is %f,((sum(TNY))/31)')
fprintf('\nAverage temperature for Anchorage is %f,((sum(TANC))/31)')
fprintf('\nThere were 17 day(s) where the temperature was below average in New York City')
fprintf('\nThere were 13 day(s) where the temperature was below average in Anchorage')
fprintf('\nThere were 11 day(s) where the temperatue in Anchorage was higher than the temperature in New York City')
fprintf('\nThese were the days: 17 9 14 15 18 19 21 22 25 26 ')
fprintf('\nThere were 1 day(s) where the temperature in Anchorage and New York City were the same')
fprintf('\nThese were the days: 23')
fprintf('\nThere were 16 day(s) where the temperature in Anchorage and New York City were above freezing')
fprintf('\nThese were the days: 7 8 9 13 14 15 16 17 18 19 20 23 24 25 26 27')
disp('Problem 6');
x = -10:2:10;
y = zeros(1, length(x));
k = 1
for i =x
if -6 <= i & i <= 2
y(k) = 4*exp(i+2);
else if -2 <= i & i <= 2
y(k) = x^2;
else if 2 <= i & i <= 6;
y(k) = (i + 62)^(1/3);
else
y(k) = 0;
end
k = k + 1;
end
plot(x,y),xlabel('x'),ylabel('y')
case 5
disp('Thank you for using this Assignment Program.')
break
end
c = input('Would you like to continue? Press Y for yes and N for no','s')
if c == 'Y' | c == 'y'
a == true
else
break
end
end
end
0 Comments
Answers (1)
Stephen23
on 9 May 2016
Edited: Stephen23
on 9 May 2016
This is a perfect example of why formatting and indenting code correctly is so important. Although beginners think that all of these so-called experts who grumpily repeat "always write comments in your code", and "always format your code neatly" and "always indent your code consistently" are just saying it to waste beginners' very precious time...
If that were the case, those so-called experts could save their breath and go for a beer in the sun instead.
The problem, in case it is not yet clear, becomes obvious when the code is formatted and indented correctly. That is why formatting and indenting code is so important: it helps humans to be able to read code, to follow its logic, and to find where problems occur.
Here is a tip: select all of your code and press ctrl+i: this will use MATLAB's automagical code indentation tool, indenting code correctly for those too lazy to do it themselves. As soon as you have done this you will find that case 5 on line 224 is not aligned with the opening switch. It seems the problem was caused by the author getting very confused by some nested if statements inside a for loop, and not putting all of the matching end statements in the right places, resulting in the case statement occurring inside the loop. Move the case outside the loop and the problem will be fixed.
Correct indentation achieves two things:
Moral of the story: grumpy professors advising long-suffering students to indent code (and write comments, etc) are not just saying this for their own sadistic reasons.
1 Comment
Jan
on 9 May 2016
I hesitate to mention this, because someone could take it personally, but some professors still rely on FORTRAN77 without code-indentation, because they learned programming when bytes like spaces have been expensive. In this case the PhD students are the ones who insist on modern programming languages and smart code styles. But without doubt, you hit the point.
See Also
Categories
Find more on Projects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!