I do not know why I am getting an error, i am trying to hand in this project but i will get a zero if the code can not be read. Please help me.
1 view (last 30 days)
Show older comments
Getting an error
Error: Function definitions are not permitted in this context.
% P2 CS101-104 1234
% first LAST
%
function p2_8145 ( NN )
fprintf('%20s %15s %-7d\n','LAST','first',1234);
approx= p2_part_a (NN);
fprintf('%-9d %10.6f\n',NN,approx);
approx= p2_part_b (NN);
fprintf('%-9d %10.6f\n',NN,approx);
approx= p2_part_c (NN);
fprintf('%-9d %10.6f\n',NN,approx);
approx=p2_part_d (NN);
fprintf('%-9d %10.6f\n',NN,approx);
[iter approx]=p2_part_e (NN);
fprintf('e: %-9d %10.6f\n',iter,approx);
end
function v1=p2_part_a (n) %PART A
v1a = 1:2:(2*n+1);
v1b = 1 ./ (v1a);
v1 = 4.*sum(v1b .* vla);
end
function v1=p2_part_b (n) %PART B
v1a=1:4:4*n+1;
v1a=1./v1a;
v1b=3:4:4*n+3;
v1b=1./v1b;
v1=minus(v1a,v1b);
v1=4*sum(v1);
end
function v1=p2_part_c (n) %PART C
v1a= 0:1:n;
v1b= (4 .* vla +1) .* (4 .* vla +3);
v1c= 2 ./ (v1b);
v1 = 4.*sum(v1c);
end
function v1=p2_part_d (n) %PART D
v1=0;
for v1a=0:1:n;
v1b=2*v1a+1;
v1c=(-1)^v1a/v1b;
v1 =v1+(-1)^ v1a / vlb;
end
v1=4*v1;
end
function [ii vl] =p2_part_e () %PART E
vl=0;
ii=0;
v1a= (1/(2*ii+1));
while v1a >= 10^(-7)
v1 = v1+(-1)^ii * vla;
ii=ii+1;
v1a=(1/(2*ii+1));
end
v1 = 4*v1;
end
0 Comments
Answers (2)
Ken Atwell
on 30 Apr 2015
You cannot put function declarations inside a MATLAB script. Cut 'n' paste each function into its own file, naming the files p2_part_a.m, p_part_b.m, and so on.
0 Comments
Image Analyst
on 30 Apr 2015
That would be permitted if it was a function in a file called p2_8145.m. But from the error message it looks like those functions are after some script in the same file. What is the name of the m file? Let's say it's called test.m. Then you can have all those functions in test.m if you put this line before any of your script lines:
function test
and since you've chosen to end your functions with the "end" keyword, you'll also need to end test() with an "end" also
function test
% Now code to call the other functions, like p2_8145().....
end
% Now other functions can be listed in the same file.
function p2_8145 ( NN )
and so on......
0 Comments
See Also
Categories
Find more on Software Development Tools in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!