the range i need for my graph plot is from -10 to 10,but it seems like i cant use a negative number.help?

1 view (last 30 days)
clc;clear all;
next = 1;
while (next > 0)
x = input(' Enter selected starting value ');
A = zeros(20,1);
B = zeros(20,1);
for count = (-10:10)
main = (5*x^3+15*x^2+x+10)*sin(x);
diff = (15*x^2+30*x+1)*sin(x)+(5*x^3+15*x^2+x+10)*cos(x);
A(count) = x;
B(count) = main;
x = x - main/diff;
if(abs(main)<1e-7)
break;
end
end
disp([' The converged value of x is ' num2str(x) ]);
disp([' The associated equation error is ' num2str(main) ]);
disp([' The number of iterations taken = ' int2str(count) ]);
semilogy( (1:count)', abs(B(1:count)) );
next = input(' Compute again ? (yes=1, no=0) : ');
end

Answers (2)

Geoff Hayes
Geoff Hayes on 27 Nov 2014
Ernest - you seem to be only using your interval of [-10,10] for the indexing variable count, which is used to update elements in an array. So you are probably getting an error message indicating that you can only use logical or positive integers to access an array element. What is the intent behind using this variable? If you are only using it to define how many iterations of the for loop to do before assuming that a solution has been found then this should be switched to
for count=1:20
% etc
end
  1 Comment
ernest
ernest on 27 Nov 2014
Geoff-I actually need to make a plot of some specific given points to that function.Will it help if I were to show you the problem statement?

Sign in to comment.


Image Analyst
Image Analyst on 27 Nov 2014
Why not just use count = 1 : 21? Here, run my code. It has a few other improvements in there also:
clc;
close all;
clear all;
fontSize = 20;
promptMessage = sprintf('Do you want to Continue and try another x,\nor Quit?');
titleBarCaption = 'Continue?';
next = 1;
while (next > 0)
x = input(' Enter selected starting value ');
A = zeros(20,1);
B = zeros(20,1);
for count = 1:21
main = (5*x^3+15*x^2+x+10)*sin(x);
diff = (15*x^2+30*x+1)*sin(x)+(5*x^3+15*x^2+x+10)*cos(x);
A(count) = x;
B(count) = main;
x = x - main/diff;
if(abs(main)<1e-7)
break;
end
end
disp([' The converged value of x is ' num2str(x) ]);
disp([' The associated equation error is ' num2str(main) ]);
disp([' The number of iterations taken = ' int2str(count) ]);
hFig = semilogy( (1:count)', abs(B(1:count)), 'LineWidth', 2);
grid on;
xlabel('Iteration Number', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Ask if they want to try again.
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
delete(gcf);
if strcmpi(button, 'Cancel')
break;
end
end
  2 Comments
Image Analyst
Image Analyst on 27 Nov 2014
Edited: Image Analyst on 27 Nov 2014
Yeah, probably for x. The "count" is the iteration number, not the x value. You need to change the x value. Since it's your homework and I can't just do the whole assignment for you outright, I'll let you do that. Hint A(1) = A at your starting x value.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!