How to save result of a function in the workspace

조회 수: 2 (최근 30일)
Gisela
Gisela 2016년 2월 21일
편집: Stephen23 2016년 2월 21일
Hi, I wrote this function that correctly runs on it's own
function theoretical1
clc;
clear all;
close all;
tau1=147
k0=[150000 1.5 1.5 1.5];
nu=[-1; -5 ;-6]; % stoichiometrical matrix of coefficients
C01 = [0.00005 0.001 0.05]; % initial concentratio of measure j
[time1,C1]= ode45(@myode1,[0 tau1],C01);
function dC1dt=myode1(t,C)
R1=k0(1)*C(1)^k0(2)*C(2)^k0(3)*C(3)^k0(4);
r1=nu*R1;
dC1dt=r1;
end
end
There is this other script I have that works perfectly until I add this second function, in which case Matlab doesn't generate an answer. I changed the first function to a m script instead so I could call the function. Problem is I don't know how to save in the workspace the values of time1 and C1 generated by the function. Is there any way to do this?
  댓글 수: 1
Stephen23
Stephen23 2016년 2월 21일
편집: Stephen23 2016년 2월 21일
Why put
clc;
clear all;
close all;
at the beginning of your function? A function's workspace starts empty, so clear all is useless. Your function also has nothing to do with figures, so why close (any) figures that you might have just plotted? Why clear your command window? What does the command window have to do with this function?
Does sin clear the command window and close all figures?:
sin(pi)
Avoid cargo-cult programming by not putting these useless, annoying commands at the start of every functions.

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

답변 (1개)

Star Strider
Star Strider 2016년 2월 21일
I am not certain that I understand what the problem is. This slight revision of your code works for me:
nu=[-1; -5 ;-6]; % stoichiometrical matrix of coefficients
k0=[150000 1.5 1.5 1.5];
myode1 = @(t,C) nu*k0(1).*C(1).^k0(2).*C(2).^k0(3).*C(3).^k0(4);
tau1=147
C01 = [0.00005 0.001 0.05]; % initial concentration of measure j
[time1,C1]= ode15s(myode1,[0 tau1],C01);
figure(1)
semilogy(time1, C1)
grid
Because your ‘k0’ constants span such an extreme range, I used ode15s.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by