Nothing but errors when writing a function to perform an integration and a driver script that calls the function and prints the results.

1 view (last 30 days)
I am trying to do an integration by using a function and a driver script that calls it.
func_integrate_viscdisc;
function sum = func_integrate_viscdisc(n, mu, h, omega, rmin, rmax)
sum = 0;
rlen = rmax - rmin;
dr = rlen/n;
for idx = 1:n;
r = rmin + dr*(idx - 0.5);
ig =
sum + sum +dr*ig;
end
end
driver_integrate_viscdisc.m
mu = 5.0E-3;
h = 2.0E-3;
omega = 100/60*2*pi;
rmin = 0.0;
rmax = 0.2;
for n = [10 100 1000];
moment = func_integrate_viscdisc(n, mu, h, omega, rmin, rmax);
fprintf('The moment computed with %4d steps is: %11.4e Nm\n', n, moment);
end
When I run it I get the error;
>> driver_integrate_viscdisc
Error: File: func_integrate_viscdisc.m Line: 7 Column: 10
Expression or statement is incomplete or incorrect.
Error in driver_integrate_viscdisc (line 8)
moment = func_integrate_viscdisc(n, mu, h, omega, rmin, rmax);
This code was copied directly from instructions provided by my professor so I have no idea what could be wrong with it.

Answers (1)

Walter Roberson
Walter Roberson on 4 Sep 2015
You have
ig =
sum + sum +dr*ig;
MATLAB is sensitive to where lines end. As far as MATLAB is concerned you have given two different input lines, one of which starts to assign a value and then abruptly ends, and the other of which calculates an expression and throws away the value of the expression. You need to merge those two into a single line, or you need to end the first line with three periods to indicate that it continues, such as
ig = ...
sum + sum +dr*ig;
Or you could fix the "ig =" line by adding something to the end of it, and then change the "sum" line into an assignment to something.
Note: I strongly recommend against naming any variable "sum", as that will interfere with using the MATLAB routine named "sum"
  2 Comments
Raxius Bloom
Raxius Bloom on 4 Sep 2015
Edited: Walter Roberson on 5 Sep 2015
Sorry, that sum line was supposed to be
sum = sum + dr*ig;
It's still not working. now it gives the error message;
>> driver_integrate_viscdisc
Error: File: func_integrate_viscdisc.m Line: 8 Column: 9
The expression to the left of the equals sign is not a valid target for an assignment.
Error in driver_integrate_viscdisc (line 8)
moment = func_integrate_viscdisc(n, mu, h, omega, rmin, rmax);
Steven Lord
Steven Lord on 4 Sep 2015
I'd like to second Walter's recommendation to avoid using sum as your variable name. Doing so will prevent you from using the SUM function in the code that defines that variable.
As for the line starting "ig = ", my suspicion (if this is part of a homework assignment) is that your instructor wants you to figure out what needs to complete that line of code to make the program work as expected. You will need to add something after the equal sign to correctly perform the integration.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!