MATLAB Answers

I am getting the error , Unable to perform assignment because the indices on the left side are not compatible with the size of the right side trying to compute the bisection m

1 ビュー (過去 30 日間)
Zachary Hills
Zachary Hills 2021 年 7 月 29 日
回答済み: KSSV 2021 年 7 月 29 日
for method_number = 1 : 3
fprintf("---\nmethod %d\n",method_number);
tol = 10^-9;
epsilon = 10^4;
steps = 1;
%% If we get in an infinite loop we'll break out of it
%% break_flag = 0 indicates the default state
%% break_flag = 1 indicates a break
break_flag = 0;
%% We'll use X to store the current and new value of x
%% at each step.
clear X;
x1 = 3.65:0.002:3.75; %% set the seed point = starting x value.
fprintf("{0, %.8f},\n",x1)
while epsilon > tol
x0 = x1; %% replace x0 with x1
x1 = method(x0,method_number); %% compute the new value of x = g(x0)
X(steps,1) = x0; %% At each step store x0 and x1 in matrix X
X(steps,2) = x1;
fprintf("{%d, %.8f},\n",steps,x1)
epsilon = abs(x1-x0);
steps = steps + 1;
%% If we're in an infinite loop or the solution is diverging
%% let's break out if it.
if steps > 200000 || epsilon == inf
break_flag = 1;
break
end
end
%% Now let's plot all of the data from our iterations
subplot(2,2,method_number)
plot(X(:,1),X(:,2)); %% plot all the data with lines
hold on
scatter(X(:,1),X(:,2)); %%replot all the data with dots
scatter(X(1,1),X(1,2),75,'g','d','filled','MarkerEdgeColor','b'); %% the starting point
scatter(X(end,1),X(end,2),75,'r','d','filled','MarkerEdgeColor','b'); %% the end point
if break_flag %% a different plot heading for divergent and convergent results
heading = sprintf('method %d: the method did not converge after %d steps',method_number,steps-1);
else
heading = sprintf('method %d: after %d steps, x = %.12f',method_number,steps-1,x1);
end
title(heading);
end
function ans = method(x,number)
switch number
case 1 %%%%% Method 1 (Example 8: x = x^2 + x - 2)
ans = exp(-x/10)-cos(x) + x; %x^2+x-2;
case 2 %%%%% Method 2 (Example 9: x = (x^2 - 5x - 2)/(-5))
k = -0.867%% try different values of k
ans = (-1/k)*(exp(-x/10)-cos(x)-k*x);
case 3 %%%%% Method 3 Newton's (Example 11: f(x)=x^2-2, f'(x)=2x, x=x-f/f')
ans = x -(exp(-x/10)-cos(x))/((exp(-x/10)*(-1/10))+sin(x));
end
end

採用された回答

KSSV
KSSV 2021 年 7 月 29 日
You are trying to save more number of elements than the matrix is initilaized. Your LHS is a scalar and RHS is a vector, so error pops out. Check the modififed code below:
function Test()
for method_number = 1 : 3
fprintf("---\nmethod %d\n",method_number);
tol = 10^-9;
epsilon = 10^4;
steps = 1;
%% If we get in an infinite loop we'll break out of it
%% break_flag = 0 indicates the default state
%% break_flag = 1 indicates a break
break_flag = 0;
%% We'll use X to store the current and new value of x
%% at each step.
clear X;
x1 = 3.65:0.002:3.75; %% set the seed point = starting x value.
fprintf("{0, %.8f},\n",x1)
X = zeros([],length(x1)) ;
Y = zeros([],length(x1)) ;
while epsilon > tol
x0 = x1; %% replace x0 with x1
x1 = method(x0,method_number); %% compute the new value of x = g(x0)
X(steps,:) = x0; %% At each step store x0 and x1 in matrix X
Y(steps,:) = x1;
fprintf("{%d, %.8f},\n",steps,x1)
epsilon = abs(x1-x0);
steps = steps + 1;
%% If we're in an infinite loop or the solution is diverging
%% let's break out if it.
if ((steps > 200000) | (epsilon == inf))
break_flag = 1;
break
end
end
%% Now let's plot all of the data from our iterations
subplot(2,2,method_number)
plot(X,Y); %% plot all the data with lines
hold on
scatter(X(:),Y(:)); %%replot all the data with dots
scatter(X(1),Y(1),75,'g','d','filled','MarkerEdgeColor','b'); %% the starting point
scatter(X(end),Y(end),75,'r','d','filled','MarkerEdgeColor','b'); %% the end point
if break_flag %% a different plot heading for divergent and convergent results
heading = sprintf('method %d: the method did not converge after %d steps',method_number,steps-1);
else
heading = sprintf('method %d: after %d steps, x = %.12f',method_number,steps-1,x1);
end
title(heading);
end
end
function ans = method(x,number)
switch number
case 1 %%%%% Method 1 (Example 8: x = x^2 + x - 2)
ans = exp(-x/10)-cos(x) + x; %x^2+x-2;
case 2 %%%%% Method 2 (Example 9: x = (x^2 - 5x - 2)/(-5))
k = -0.867%% try different values of k
ans = (-1/k)*(exp(-x/10)-cos(x)-k*x);
case 3 %%%%% Method 3 Newton's (Example 11: f(x)=x^2-2, f'(x)=2x, x=x-f/f')
ans = x -(exp(-x/10)-cos(x))/((exp(-x/10)*(-1/10))+sin(x));
end
end

その他の回答 (0 件)

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by