Graphing two plots on the same window seperately

조회 수: 2 (최근 30일)
Mark Grano
Mark Grano 2012년 10월 6일
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!

채택된 답변

Image Analyst
Image Analyst 2012년 10월 6일
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
  댓글 수: 4
Mark Grano
Mark Grano 2012년 10월 7일
the diaglog box would be find, im not opposed to that, its just that when i presss continue i need it to completely re run the program, starting with asking for new inputs for num,x0, and x1. with the code i have now, when i enter 'y' it does not re run the program for some reason, it just stops it.
Image Analyst
Image Analyst 2012년 10월 7일
If you want to go to the start of the while loop, then use "continue" instead of "break".

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 10월 6일
편집: Walter Roberson 2012년 10월 6일
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"...)
  댓글 수: 5
Image Analyst
Image Analyst 2012년 10월 6일
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
Mark Grano 2012년 10월 6일
hmm that still doesn not allow me to rerun the program, it just gives me the option and when i click yes, the program stops

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by