Parse error not understandable in this code

23 Views Asked by At

In this program, it says that I have a parse error at line line 16, disp("L'angle est aigu") but I don't understand where it is coming from.

function w = prog1(u,v);
  n = length(u);
  m = length(v);
  if n != m
    disp('Produit scalaire impossible.');
  else
    v = v / norm(v);
    ps = 0;
    for i = 1:n
      ps = ps + u(i)*v(i);
    endfor
    disp(["Le produit scalaire est :",num2str(ps)])   
    angle = acosd(ps/(norm(v)*norm(u)))               
    disp(["L'angle, en degré, entre les deux vecteurs est :",num2str(angle)])   
    if angle < 90:
      disp("L'angle est aigu")
    elseif angle == 90:
      disp("L'angle est droit")
    else :
      disp("L'angle est obtu")
    endif
    w = ps *v;
  endif
endfunction

I tried end, endif, and I believe it is where it should be but it still doens't work. Help me !

1

There are 1 best solutions below

1
Nick J On

The syntax error in your program does not appear to be related to the single quotes in your disp statements. It is because of the unnecessary : at the end of your if, elseif, and else statements. Remove those, and your function should work:

>> prog1(1,2)
Le produit scalaire est :1
angle = 0
L'angle, en degré, entre les deux vecteurs est :0
L'angle est aigu
ans = 1

If the single quote inside the double quoted string was an issue, you could use the \' escape sequence, but it does not seem to be an issue here. E.g., changing:

disp("L'angle est aigu")

to

disp("L\'angle est aigu")

in all of the disp lines, you get the same output:

>> prog1(1,2)
Le produit scalaire est :1
angle = 0
L'angle, en degré, entre les deux vecteurs est :0
L'angle est aigu
ans = 1