Effacer les filtres
Effacer les filtres

Trouble with quiver function and plotting a vector vield

9 vues (au cours des 30 derniers jours)
Michael Hay
Michael Hay le 2 Juil 2021
I am having troubles getting this script to run I am new to matlab so if you could edit it for me that would be helpful! Thanks!
I am getting the error:
Error using quiver (line 44)
The size of X must match the size of U or the
number of columns of U.
Error in DirectionField (line 33)
quiver(t,y,tc./arrow,yc./arrow,0.50,'b') % Plots
all scaled vectors (in blue)
function DirectionField()
% This program creates the direction field for
% the first order DE dy/dt = t^2/(1-(y^2))
t=[0:1:10]; %t The interval and step size between points
y=[-10:1:10]; %y The interval and step size between points
m=length(t);
n=length(y); % These together establish the size of the vector field
tc=zeros(m,n); % Creates t values for the mxn grid, all initially zero
yc=zeros(m,n); % Creates y values for the mxn grid, all initially zero
% This "double for loop" creates a matrix of all vectors in the field
% by storing each vector's t and y components separately
for i=21:m
for j=21:n
tc(j,i)=0;
%%yc(j,i)=(t(i).^2)./(1-(y(j).^2));
yc(j,i)=-((0.25^2)/14)*sqrt(2*9.8*y);
end;
end;
figure(1)
plot(t,0*t,':k') % Plots the t-axis only
plot(0*y,y,':k') % Plots the y-axis only
arrow=sqrt(tc.^2 + yc.^2); % This generates the vector size for each vector
quiver(t,y,tc./arrow,yc./arrow,0.50,'b') % Plots all scaled vectors (in blue)
% to create desired direction field
hold on
axis tight % Chooses an appropriate window size based on what is plotted
end

Réponses (1)

Cris LaPierre
Cris LaPierre le 2 Juil 2021
I don't understand what you want your for loops to do. I think you have set them up incorrectly. You should check out Ch 13 of MATLAB Onramp, which covers them. While there you may want to check out some of the other chapters. Ch 11 covers plotting, including how to correctly use hold.
For quiver, you appear to be using this syntax
where X and Y are vectors, and U and V are matricies. Take note of the following input details (from the documentation)
"If X and Y are vectors and U and V are matrices, then quiver expands X and Y into matrices. In this case, size(U) and size(V) must equal [length(Y) length(X)]."
When I tried running your function, I got an error stating that size of X must match the number of columns of U. It currently matches the number of rows.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by