Graphing two plots on the same window seperately
Afficher commentaires plus anciens
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!
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 6 Oct 2012
Modifié(e) : Walter Roberson
le 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"...)
5 commentaires
Mark Grano
le 6 Oct 2012
Modifié(e) : Walter Roberson
le 6 Oct 2012
Walter Roberson
le 6 Oct 2012
Modifié(e) : Walter Roberson
le 6 Oct 2012
Order of operations is left to right
while iter<1000 && (h=='y' || h=='Y')
Caution: using strcmp() is recommended for comparing strings.
Mark Grano
le 6 Oct 2012
Image Analyst
le 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
le 6 Oct 2012
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!