필터 지우기
필터 지우기

How to make a graph of an equation if part of it is an array?

조회 수: 1 (최근 30일)
DEHAO Lin
DEHAO Lin 2020년 12월 18일
댓글: DEHAO Lin 2020년 12월 19일
I am trying to make a graph of y, but I am getting "Unable to perform assignment because the left and right sides have a different number of elements." most of the time. I think the array r is the reason I am getting this problem, but I don't know how to make that graph happen. Can someone show my a path to solve this or provide some advice?
h = 1;
t = 1 : h : 100;
r = 88 - 3.5*t;
f = @(t,N) (r)*N*(1-N/51000000000);
y = zeros(1,length(t));
y(1) = 10;
for ii = 2:length(t)
y(ii) = y(ii-1)+ h*feval(f,t(ii-1),y(ii-1));
end
plot(t,y);

채택된 답변

VBBV
VBBV 2020년 12월 18일
편집: VBBV 2020년 12월 18일
%i
h = 1;
t = 1 : h : 100;
r = 88 - 3.5*t;
f = @(t,N) (r)*N*(1-N/51000000000);
y = zeros(length(t),length(t));
y(:,1) = 10;
for ii = 1:length(t)
y(ii,:) = y(ii,:)+ h*feval(f,t(ii),y(ii));
end
plot(t,y);
Try this
  댓글 수: 2
VBBV
VBBV 2020년 12월 18일
You can also try with this initial condition
%f true
y(1,:) = 10;
DEHAO Lin
DEHAO Lin 2020년 12월 19일
Thanks for the help! It works although it was not I was expected, maybe since I introduced the r(t) I should have make it a f(x,y,z) and a 3D graph. I haven't learn that in Matlab yet but I maybe soon in the future. Thanks again for finding a working code!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Daniel Pollard
Daniel Pollard 2020년 12월 18일
You're right, the right side of line 10 has the same size as r (which itself has the same size as t), and you're trying to assign it to an element of a vector. You can't set a 1x1 object to equal a 100x1 object in matlab.
You can give f another argument, r, and then in feval call give it an argument of r(ii), or something like that. When I tried that y became mostly NaN or -Inf. I don't know if that's what you expected or not though.
  댓글 수: 1
DEHAO Lin
DEHAO Lin 2020년 12월 19일
I don't know what NaN or -Inf, but I decided to replace the r(t) with a constant so that simplified the issue. I was just trying to have a graph that can show equilibrium. Thanks for the help!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by