Iteration durchführen anhand einer For Schleife

11 次查看(过去 30 天)
Hallo,
ich möchte die unten stehende Funktion numerisch untersuchen und möchte das ganze mit einem Iterationsverfahren aneghen zur Bestimmung des Fixpunktes. Nun gibt es zwei erstellte Funktionen, die mit dem Wert 1.5 anfangen sollen und beobachtet werden soll, wie sich die Werte bezüglich des Fixpunktes verhalten. Nachdem ich den Code laufen lasse, kommt folgende Meldung:
"Unable to perform assignment because the left and right sides have a different number of elements.
Error in Num (line 24)
x(i+1) = nthroot(2*x+0.5*sin(x),3); "
Könnt Ihr bitte helfen?
%alle Variablen, Fenster und Command Window schließen
clear all;
close all;
clc;
%Funktion F(x) und ihre positive Nullstelle
fun = @(x)-x.^3+2*x+0.5*sin(x);
x0 = 2;
fzero(fun,x0)
x=-3:0.1:3;
y=-x.^3+2*x+0.5*sin(x);
a=x-x;
plot(x,a)
hold on;
plot(x,y)
xlabel('x'); ylabel('y');
hold on;
plot(fzero(fun,x0),0,'o')
hold on;
legend('x-Achse' , 'F(x)','Nullstelle')
%Startwert
x(1)=1.51
%erste Umformung
for i = 1 : 15
x(i+1) = nthroot(2*x+0.5*sin(x),3);
end
k_1 = x(i)
%zweite Umformung
for i = 1 : 20
x(i+1) = ((x.^3-0.5*sin(x))/2);
end
k_2 = x(i)
  2 个评论
madhan ravi
madhan ravi 2023-11-16
Die bestehende Fehlermeldung tritt aufgrund der Dimension der Variable x auf. Du versuchst, einen Wert mit mehreren Werten zu belegen. Leider funktioniert das nicht mit einem numerischen Array. Daher muss man immer den Index in der For-Schleife wie in der gegebenen Antwort verwenden.
Ahmet Cin
Ahmet Cin 2023-11-16
Vielen Dank, habe jetzt den Fehler gefunden. Da haben die Indizes (i) gefehlt.

请先登录,再进行评论。

回答(1 个)

Dyuman Joshi
Dyuman Joshi 2023-11-16
移动:Dyuman Joshi 2023-11-16
%Funktion F(x) und ihre positive Nullstelle
fun = @(x)-x.^3+2*x+0.5*sin(x);
x0 = 2;
y0 = fzero(fun,x0)
y0 = 1.5256
x=-3:0.1:3;
y=-x.^3+2*x+0.5*sin(x);
a=x-x;
plot(x,a)
hold on;
plot(x,y)
xlabel('x'); ylabel('y');
hold on;
plot(y0,0,'o')
hold on;
legend('x-Achse' , 'F(x)','Nullstelle')
%Startwert
x1=1.51;
%erste Umformung
for i = 1 : 15
x1(i+1) = nthroot(2*x1(i)+0.5*sin(x1(i)),3);
end
x1
x1 = 1×16
1.5100 1.5210 1.5243 1.5252 1.5255 1.5256 1.5256 1.5256 1.5256 1.5256 1.5256 1.5256 1.5256 1.5256 1.5256 1.5256
k_1 = x1(end)
k_1 = 1.5256
x2 = 1.51;
%zweite Umformung
for i = 1 : 20
x2(i+1) = ((x2(i).^3-0.5*sin(x2(i)))/2);
end
x2
x2 = 1×21
1.5100 1.4719 1.3458 0.9750 0.2565 -0.0550 0.0137 -0.0034 0.0009 -0.0002 0.0001 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000
k_2 = x2(end)
k_2 = 5.0847e-11

产品

Community Treasure Hunt

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

Start Hunting!