Index exceeds the number of array elements (1)
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
Im doing an assignment where my script should repeats forward euler for different step sizes h between 0.1V/g and 0.001V/g, doubling the number of steps taken each time,and making sure the final time is exactly 3V/g.
function [z,v,studentid] = particle(m,g,k,p,V,h,N)
Above is the function I use for forward euler, and below is the code in another script where im trying to change the step size.
end
%Repeating for different h and N
[z2,v2,~]=particle(5,9.4,0.1,1.2,30,0.1*V/g,10);
[z3,v3,~]=particle(5,9.4,0.1,1.2,30,0.075*V/g,20);
[z4,v4,~]=particle(5,9.4,0.1,1.2,30,0.05*V/g,40);
[z5,v5,~]=particle(5,9.4,0.1,1.2,30,0.025*V/g,80);
[z6,v6,~]=particle(5,9.4,0.1,1.2,30,0.001*V/g,160);
%error
for x=[2 6]
errorz2=z(x)-z;
errorv2=v(x)-v;
end
I feel like ive done it a completely harder way than I need to and wondered if I could simplify it somehow. The assignment also asks me to plot two clearly labelled graphs (using the subplot command), of the magnitude of the error versus h, on log-log axes.
The error for each line is just z(x)-z as it says below, but when i try to run that it says 'Index exceeds the number of array elements (1)'
If someone could please help me that would be so so amazing, I have hit a wall with this.
Thanks :)
0 commentaires
Réponses (1)
Fangjun Jiang
le 27 Nov 2019
Modifié(e) : Fangjun Jiang
le 27 Nov 2019
You need to replace z with z(1), z2 with z(2), z6 with z(6). Similarly for v, v2, ... v6.
Some fundermental MATLAB basics:
z=rand(1,6) creates a 1x6 vector, you can refer the element, z(1), z(2), ... z(6)
while z1, z2, ... z6 are totally different variables.
0 commentaires
Voir également
Catégories
En savoir plus sur Title dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!