How is it possible that matlab solves two simultaneous equations which have relation to each other? I am very new with matlab and can not get it done. I want to solve several equations with the euler method bot always get errors.

main code :
clear all
a=0;
b=10;
N=10;
y0=0;
h=(b-a)/N;
t(1)=a;
R(1)=1;
Pg(1)=1;
P1(1)=1;
c(1)=12;
for n=1:N;
t(n+1)=t(n)+h;
c(n+1)=c(n)+h*g(t(n),c(n));
Pg(n+1)=1.9*g(t(n),c(n));
P1(n+1)=8*R(n);
R(n+1)=R(n)+h*f(t(n),R(n)) ;
end
plot(t,R)
function g:
function g=g(t,c)
g=(45*c)/74;
function f:
function f=f(t,R)
f=(((Pg-P1)*R)/152)-60;
this is the result:
Error :Undefined function or variable 'Pg'
Error in f (line 2)
f=(((Pg-P1)*R)/152)-60
how can I asign the variable Pg and P1 to the equation in the main code

댓글 수: 4

There are a lot of problems with this code. I would suggest working through some tutorials.
You are new to MATLAB and trying to jump in head first, without understanding how MATLAB works. I recommend taking it one step at a time. Strat with easier examples and build up to this problem.
To start with your function declarations are not valid.
Functions need to be defined like this:
function [output1, output2] = functionName(input1,input2)
% Code for the functio goes here
where this example shows 2 outputs and 2 inputs (you can have more or less if you need). This cannot be defined at the command line and cannot be defined in a script file. In the Home tab of MATLAB, select the arrow beneath New (has a + icon) and select function. This will give you the template for writing a file. My guess is there will be more problems you encounter after this, so just take it one step at a time. I recommend experimenting with easier examples first and as you run into issues post them.
Use the documentation as much as possible.
docsearch create a function
There are other online resources to get you started:
Brendan, I think I may have caused some confusion by reformatting the code incorrectly (I have fixed the error). I think they already are in separate files. Sorry about that.
Thanks for the comments! The functions were in different files, but thanks for the detailed response and links. I realize, I'm still stuck with many problems here, but unfortunately I'm very impatient:-) Iam happy for every help I can get.

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

 채택된 답변

You could just write
Pg = 1;
but a better way, since you know how large the vector will be, is to preallocate the memory:
Pg = ones(1,N+1);

댓글 수: 2

First off, thanks for the reply. The code calculates a result when I sat Pg to 1 in the function f . However, my goal would be that after each cycle of the loop the Pg value from the large file gets transfered to the function file. When I change the constant in
Pg(n+1)=1.9*g(t(n),c(n)) ...
the R(n+1) does not change. Thx
Sorry, I realize now that I didn't really answer your question. The reason for the error is that you are not passing the values of Pg and P1 to f. One approach is to add them as input parameters. Or you could combine them as below:
R(n+1)=R(n)+h*f(t(n),(Pg(n)-P1(n))*R(n)) ;
The function is:
function f=f(~,x)
f=(x/152)-60;
Note the tilde. It's good practice to do this when an input variable (in this case t) is not used.
I ran this modified code and it produced a plot.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2015년 3월 12일

댓글:

2015년 3월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by