Resolving errors with Raphson method
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Alexander
il 17 Set 2023
Commentato: Star Strider
il 17 Set 2023
Hello,
I am having difficulties in writing a Raphson method using a while loop. Matlab keeps returning error: y(0): "subscripts must be either integers 1 to (2^63)-1 or logicals". I have tried to change my code several times, to try to solve this problem, but I have been unsuccesful. I am wondering if anyone has any suggestions on how to solve this problem?
4 Commenti
Dyuman Joshi
il 17 Set 2023
You have to loop over the values [1 2 3 ... length(x)], right? If yes, why not use a for loop? Because you are not breaking out of the while loop if a condition is met.
Also, I don't understand why you are adding x(i) to i, in the last line of the while loop.
Risposta accettata
Star Strider
il 17 Set 2023
My pleasure!
The ‘y’ assignment needs to be an anonymous function, if I understand correctly what you want to do.
That change produces this result —
L = 1;
x = linspace(0,1,100);
y = @(x) (8*x)/(3*L) - 3*(x/L).^2 + (1/3)*(x/L).^3 - (2/3)*sin((pi*x)/L); % Anonymous Function
uy = 1/6*y(x);
figure
plot(x, y(x), 'DisplayName','y(x)')
hold on
plot(x, uy, 'DisplayName','uy(x)')
hold off
grid
xlabel('x')
ylabel('Function Values')
legend('Location','best')
See the documentation on Anonymous Functions for details. Call them as you would any other function. Similar to variables, they must be defined before they are used in the code they are implemented in.
.
2 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
