Asked by Mark Grano
on 6 Oct 2012

Hi im having a bit of trouble graphing two seperate graphs in the same window. I get an error:

Error using * Inner matrix dimensions must agree.

Error in subplot (line 307) elseif max(plotId) > nCols * nRows

Error in Secantmethod (line 35) subplot(x,f,1);

and i am not sure how to take this.

clear;clc;

hold on

grid on

num=input('Enter an integer between 2 and 15: ');

iter=0;

x=linspace(-10,10,500);

maxiter=1000;

if (num<2 || num>15 || rem(num,1)~=0)

disp('Invalid input'); num=input('Enter an INTEGER between 2 and 15: ');

end

f=( ((sqrt(log(x.^(2) + 1)))/ (cosh(x)) - x + pi - (20/3)*exp(-abs(x/10))));

x0= input( 'Please enter a value for x0: ');

x1= input( 'Please enter a vlaue for x1: ');

f0=( ((sqrt(log(x0^(2) + 1)))/ (cosh(x0)) - x0 + pi - (20/3)*exp(-abs(x0/10))));

f1=( ((sqrt(log(x1^(2) + 1)))/ (cosh(x1)) - x1 + pi - (20/3)*exp(-abs(x1/10))));

while (iter<1000)

x2= x1-f1 * [(x0 -x1)/ (f0-f1)];

f2=( ((sqrt(log(x2^(2) + 1)))/ (cosh(x2)) - x2 + pi - (20/3)*exp(-abs(x2/10))));

diff= abs(x2-x1);

if (diff < 10^(-num))

root=x2;

disp(root);

subplot(x,f,1);

subplot(x2,diff,2);

else x0=x1; x1=x2; f0=f1; f1=f2; end iter=iter+1; end if (iter==maxiter) disp('Convergence was not achieved'); end

i know my problem is in the if statement with subplots but i don't know what i need to do. i was thinking if i change diff to diff(iter) it would help but that gives me yet another error. I would really appreciate some input on how i could get these two things to graph. Thank you!

Answer by Image Analyst
on 6 Oct 2012

Accepted Answer

Mark, OK try this:

clc; % Clear the command window. close all; % Close all figures (except those of imtool.) imtool close all; % Close all imtool figures. clear; % Erase all existing variables. workspace; % Make sure the workspace panel is showing. format longg; format compact; fontSize = 14;

num=input('Enter a power of 10 between 2 and 15: '); iter=0; x=linspace(-10,10,500); maxiter=1000; if (num<2 || num>15 || rem(num,1)~=0) disp('Invalid input'); num=input('Enter an INTEGER power of 10 between 2 and 15: '); end f=( ((sqrt(log(x.^(2) + 1)))/ (cosh(x)) - x + pi - (20/3)*exp(-abs(x/10)))); x0= input( 'Please enter a value for x0: '); x1= input( 'Please enter a vlaue for x1: '); f0=( ((sqrt(log(x0^(2) + 1)))/ (cosh(x0)) - x0 + pi - (20/3)*exp(-abs(x0/10)))); f1=( ((sqrt(log(x1^(2) + 1)))/ (cosh(x1)) - x1 + pi - (20/3)*exp(-abs(x1/10)))); convergenceLimit = 10^(-num); while (iter<1000) x2= x1-f1 * [(x0 -x1)/ (f0-f1)]; f2=( ((sqrt(log(x2^(2) + 1)))/ (cosh(x2)) - x2 + pi - (20/3)*exp(-abs(x2/10)))); iterationDifference = abs(x2-x1); fprintf('%d, difference = %f\n', iter, iterationDifference); if (iterationDifference < convergenceLimit) root=x2; disp(root); subplot(2,1,1); scatter(x,f); xlabel('x', 'Fontsize', fontSize); ylabel('f', 'Fontsize', fontSize); title('f vs. x', 'Fontsize', fontSize); grid on hold on subplot(2,1,2); scatter(x2,iterationDifference); xlabel('x2', 'Fontsize', fontSize); ylabel('iterationDifference', 'Fontsize', fontSize); title('Difference vs. x2', 'Fontsize', fontSize); grid on hold on % Enlarge figure to full screen. set(gcf, 'units','normalized','outerposition',[0 0 1 1]); msgbox('Please see the plots.'); break; else x0=x1; x1=x2; f0=f1; f1=f2; end message = sprintf('That was iteration %d, with a difference of %f\nDo you want to continue?',... iter, iterationDifference); button = questdlg(message, 'Continue?', 'Yes', 'No', 'Yes'); drawnow; % Refresh screen to get rid of dialog box remnants. if strcmpi(button, 'No') break; end iter=iter+1; end if (iter >= maxiter) disp('Convergence was not achieved'); end

Image Analyst
on 6 Oct 2012

Hmmm... looks like you totally ignored my code. Any reason why you don't like the nice dialog box to ask the user to continue or quit? Here is some primitive 1970's like code that you can use:

h=input('Enter y to continue. All else quits. ', 's'); if lower(h) ~= 'y' fprintf('Quitting...\n'); break; else fprintf('Continuing...\n'); end

Mark Grano
on 7 Oct 2012

Image Analyst
on 7 Oct 2012

If you want to go to the start of the while loop, then use "continue" instead of "break".

Log in to comment.

Answer by Walter Roberson
on 6 Oct 2012

Edited by Walter Roberson
on 6 Oct 2012

You are using subplot() incorrectly. subplot() should not be called with data values as its three arguments: it should be called with values that indicate the arrangement of plots to use and which particular plot in the grid you want to use. For example,

subplot(3,5,2)

would select the 2nd plot in a virtual 3 x 5 grid of plots.

Change your

subplot(x,f,1); subplot(x2,diff,2);

to

subplot(2,1,1); scatter(x,f); hold on subplot(2,1,2); scatter(x2,diff); hold on

Also, while you are editing your file, please rename your variable "diff" to something else that is not the name of a built-in MATLAB function; using a variable name which is a common function name almost always leads to problems. (The number of times here we've had people whose program broke because they used a variable named "sum"...)

Mark Grano
on 6 Oct 2012

Image Analyst
on 6 Oct 2012

How about this instead:

message = sprintf('Do you want to continue'); button = questdlg(message, 'Continue?', 'Yes', 'No', 'Yes'); drawnow; % Refresh screen to get rid of dialog box remnants. if strcmpi(button, 'No') break; end

Mark Grano
on 6 Oct 2012

Log in to comment.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments

Log in to comment.