Need help calculating a system of ODE's using the forward Euler method!

2 vues (au cours des 30 derniers jours)
Shaun Devonshire
Shaun Devonshire le 23 Mar 2020
Commenté : John D'Errico le 23 Mar 2020
Help!
Have been given an assingment and am stuck on the start of the question, and have no previous examples to help me with. The following is the basis for my question which im stuck on. Any help would be greatly appreciated.
  3 commentaires
John D'Errico
John D'Errico le 23 Mar 2020
There are several errors that new users make. But it is difficult to know which, as the code you show generally looks reasonable. The most obvious problem seems to be in the use of the beta function, which suggests to me that you have a problem there, as most likely, you may not even know you were trying to use the beta function since it was called incorrectly.
When you get an error, show the ENTIRE text of the errror message in red. That typically allows people to know what you are doing wrong.
Shaun Devonshire
Shaun Devonshire le 23 Mar 2020
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in
the second matrix. To perform elementwise multiplication, use '.*'.
Error in q2_1705566>Fun (line 22)
f1_dot = y*(1-(y/1000)-(x/1000));
Error in q2_1705566 (line 12)
y(i+1,:) = y(i,:) + h * Fun (x,y(i,:));
the issue is i have tried eliminating the beta functions and the max values by inputting their values (in the question it says that the growth rate is 1 and the max for the pop. is 1000), but even when i did this it came back with the same error code.

Connectez-vous pour commenter.

Réponses (1)

darova
darova le 23 Mar 2020
I have some tips:
These a constants, not functions. YOu should declare them
betax = 1;
betay = 1;
maxx = 1000;
maxy = 1000;
You are using x and y in a wrong way. Look
  1 commentaire
John D'Errico
John D'Errico le 23 Mar 2020
To add a few points:
Your immediate problem is in not using the correct operator. y is a vector, of length 2. You wrote this:
f1_dot = y*beta(y)*(1-(y/max(y))-(x/max(x)));
Or, now this:
f1_dot = y*(1-(y/1000)-(x/1000));
In either case, on the face of things, seems vaguely logical. In fact though, you have a problem.
y is avector. When you want to erform a multiplication where you want to multiply every element of two vectors, you need to use .* not * as an operator.
For example:
X = 1:3
X =
1 2 3
>> X*X
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows
in the second matrix. To perform elementwise multiplication, use '.*'.
>> X.*X
ans =
1 4 9
The same rule applies to powers of vector elements, and when you divide one vector by another. Additions and subtractions are not a problem. And you can multiply a vector by an scalar using the * operator. But otherwise, get used to using .* where that is appropriate.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical 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!

Translated by