Trouble with ez-plot

18 views (last 30 days)
Adam Anderson
Adam Anderson on 29 Jan 2012
I'm writing a program to find principle stresses as assigned in a class that I am taking. I am trying to use ezplot to solve for areas to find the roots of the stresses. I keep running into trouble with this because the program keeps printing an error message about elements in I must equal B. I used it earlier to plot a quadratic and it worked fine. I tried to read some posts about this but it didn't make much sense. Any help would be greatly appreciated in advance. How do I check the sizes of the elements in I and in B? I started working exclusively with the string that was giving me the trouble. Here is the code along with the command I issue in the command window.
function [ z ] = Untitled3(sig )
sig=-200:200;
z=sig.^3-.120*sig.^2-11.8*sig-(-844)
end
sig=(-200:200);ezplot(@(z)Untitled3(sig),[-20,20])
I took three zeros off the coefficients of sig trying to help but it doesn't seem to matter. The error message displayed is this: ??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> ezplotfeval>applyfun at 79 z(i) = feval(f,x(i));
Error in ==> ezplotfeval at 66 z = applyfun(x);
Error in ==> ezplot>ezplot1 at 472 [y, f, loopflag] = ezplotfeval(f, x);
Error in ==> ezplot at 146 [hp, cax] = ezplot1(cax, f{1}, vars, labels, args{:});
Any help would be appreciated. And if you could explain in layman's terms that would be great too.

Accepted Answer

Andrew Newell
Andrew Newell on 29 Jan 2012
The syntax of your command is wrong. This is how you do it:
f = @(sig) sig.^3-.120*sig.^2-11.8*sig-(-844);
ezplot(f,[-200,200])
The domain you're plotting over should not appear inside the function. You could also define your function in a separate file as follows:
function y = f(x)
y = x.^3 - .120*x.^2 -11.8*x-(-844);
By the way - do you really mean to write -(-844), which is the same as +844?
  3 Comments
Adam Anderson
Adam Anderson on 29 Jan 2012
wow. The syntax I was using is something similar to what the teacher posted in our class notes.
Andrew Newell
Andrew Newell on 29 Jan 2012
You might find this page useful: http://www.mathworks.com/help/techdoc/learn_matlab/f4-2525.html. It's part of the "Getting Started" section of the MATLAB documentation.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 29 Jan 2012
Do not overwrite sig inside of Untitled3 . ezplot is going to call Untitled3 with however many elements it wants in the arguments that it feels like, but because you ignore the argument passed in and use your own sig, you always return 401 outputs instead of the size of the argument that ezplot() passes in.
  3 Comments
Walter Roberson
Walter Roberson on 29 Jan 2012
You *can* use the input or output arguments elsewhere in a function, and there are good uses for that sometimes.
However, when you do it, you have to be careful that the size of whatever you do return matches the size expected by the calling function. Also if you assign a value to the output parameter, you have to be careful in case you end up having an early "return" statement, or in case a section of code you are thinking will always write in a new value does not *always* get executed. It isn't that these things cannot be programmed, but you need to take care when you do the programming.
You _can_ declare a function with no outputs:
function myfun(x)
[...]
end
If, though, this gets invoked in a circumstance where the caller expects an output value, then you will get an error.
Also, if you have a function that has no outputs, then the only purpose in calling the function is for "side effects" -- e.g., values printed, global variables changed, shared variables changed, object properties set(). Functions of this nature, executed for their side effects, occur quite often when programming GUIs, but otherwise are _mostly_ used for printing values (or some Object Oriented Programming purposes.)
It does not appear to me that using a function with no outputs would be useful in your circumstances.
Andrew Newell
Andrew Newell on 29 Jan 2012
Adam, you can't accept both answers but you can vote for Walter's.

Sign in to comment.

Categories

Find more on MATLAB Mobile Fundamentals in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!